Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Unmerge All Cells in a Sheet

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To unmerge every merged cell on a worksheet with VBA, run a single line: ActiveSheet.Cells.UnMerge. That clears all merged ranges on the active sheet at once, leaving the value that was in the top-left cell of each merged area in that top-left cell and leaving the other cells blank. If you want to keep the value in every cell of the former merge, loop over the cells first and copy the top-left value into each one before unmerging. The macro is safe to run on a copy of your data, but it cannot be undone with Ctrl+Z, so save the workbook first.

What You'll Need

  • Excel for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the code, or a throwaway copy if you only need a one-off cleanup.
  • Basic familiarity with the VBA Editor: Alt+F11 on Windows, or Fn+Option+F11 on many Macs, also available via Developer > Visual Basic.

Step-by-Step

  1. Save a copy of the workbook. Unmerging is not reversible with Undo once the macro runs, so keep an untouched backup before you start.
  2. Open the VBA Editor. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac), or click Developer > Visual Basic. Menu labels can differ slightly by Excel version, device and region.
  3. Insert a module. In the Project Explorer, right-click your workbook name, choose Insert > Module. A blank code window opens.
  4. Paste the basic macro. Type or paste:
    Sub UnmergeAllCells()
        ActiveSheet.Cells.UnMerge
    End Sub
    This targets the sheet that is active when you run it.
  5. Run it. Place the cursor inside the subroutine and press F5, or close the editor and use Developer > Macros, select UnmergeAllCells, and click Run. Every merged range on the active sheet is now split.
  6. Check the result. Look for the tell-tale sign of a former merge: a value sitting only in the top-left cell with blank neighbours. If you expected the value repeated across the former merge, use the fill-down version below.
  7. Use the fill-down version when you need the value in every cell. Replace the macro with:
    Sub UnmergeAndFill()
        Dim c As Range
        For Each c In ActiveSheet.UsedRange
            If c.MergeCells Then
                c.MergeArea.Value = c.Value
            End If
        Next c
        ActiveSheet.Cells.UnMerge
    End Sub
    This writes the top-left value into every cell of each merged area, then unmerges. It is the version most people want when preparing data for sorting, filtering or a PivotTable.
  8. Run it on every sheet in the workbook, if needed. Use:
    Sub UnmergeAllSheets()
        Dim ws As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            ws.Cells.UnMerge
        Next ws
    End Sub
    This skips nothing, including hidden sheets, so confirm you want that before running.
  9. Save as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). A standard .xlsx file will drop the code on close.

Common Mistakes

  • Expecting Undo to work. VBA actions bypass Excel's undo stack. If the result is wrong, close without saving and reopen your backup.
  • Running on the wrong sheet. ActiveSheet means whatever sheet is visible at that moment. Click the intended tab first, or change the code to Worksheets("Sheet1").Cells.UnMerge.
  • Losing data from the non-top-left cells. Excel only keeps the top-left value of a merged range. If someone typed into a merged area expecting all cells to hold data, that data was never stored separately and cannot be recovered by unmerging.
  • Forgetting protected sheets. Unmerging fails on a protected worksheet. Unprotect it first (Review > Unprotect Sheet) if you have the password.
  • Assuming it fixes layout. Unmerging does not resize columns or restore borders. Column widths and formatting stay as they were, so tidy up afterwards.

Tips

  • To find merges before removing them, use Home > Find & Select > Go To Special > Merged cells. That highlights every merged range on the sheet.
  • If you only want to unmerge a specific range, replace Cells with that range, for example Range("A1:D20").UnMerge.
  • Add Application.ScreenUpdating = False at the start and True at the end for large sheets; it speeds up the loop noticeably.
  • Keep merged cells out of data tables entirely. Merging breaks sorting, filtering and PivotTables, and is the usual reason a spreadsheet misbehaves later.
  • Test macros on a copy until you are confident. A one-line change to the range can affect far more cells than intended.

Troubleshooting

  • "Run-time error 1004" or a protection message. The sheet is protected or the workbook is shared. Unprotect the sheet, or unshare the workbook via Review > Share Workbook, then retry.
  • Nothing happens. Check the macro ran on the sheet you expected. If you used a specific sheet name in code, confirm the tab name matches exactly, including spaces.
  • The macro is missing from the list. Macros in a standard .xlsx are not saved. Save as .xlsm, and check that macros are enabled in the Trust Center.
  • Values disappeared. Restore the backup. The most likely cause is that the merged area held a value only in the top-left cell, which is normal Excel behaviour, or that the fill-down version overwrote existing data in neighbouring cells.
  • It is very slow. The fill-down loop touches every used cell. Limit the range to the actual data area, or switch off screen updating as described in Tips.

Frequently Asked Questions

Does unmerging keep the data?

Yes, but only the value in the top-left cell of each merged range survives in that cell. The other cells become blank. Use the fill-down macro if you need the value copied across.

Can I undo a VBA unmerge?

Not with Ctrl+Z. Close the workbook without saving and reopen the saved copy, which is why saving a backup first matters.

Will this work on a protected sheet?

No. Unmerge requires the sheet to be unprotected. Remove protection first if you have the password.

Can I unmerge only part of a sheet?

Yes. Use Range("A1:D20").UnMerge instead of Cells.UnMerge to limit the action to that block.

Does unmerging affect formatting?

Borders and fills applied to the merged range usually remain on the individual cells, but column widths do not change. Expect to adjust layout afterwards.

Can I run this on all sheets at once?

Yes, loop through ThisWorkbook.Worksheets as shown in step 8. Remember it includes hidden sheets.