Smart Systems for Home

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

Excel VBA: How to Ungroup Cells with a Macro

· 7 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 ungroup cells in Excel VBA, use the Ungroup method on a range: Selection.Ungroup removes one level of grouping from the selected cells, while Cells.Ungroup removes grouping from the entire worksheet. For merged cells, use Range("A1:C3").MergeCells = False or Selection.UnMerge instead, because merged cells and outline groups are two different things and VBA treats them separately.

That distinction is the single most common source of confusion. Excel has two features that both get called "grouping": outline groups (the plus and minus buttons in the margin, created with Data > Group) and merged cells (created with Home > Merge & Center). The Ungroup method only touches outline groups. If your cells are merged and you run Ungroup, nothing happens and no error appears, which makes it look like the macro is broken. This guide covers both cases, plus how to loop through a whole workbook when you have many sheets to clean up. Menu labels can differ slightly by Excel version and platform, but the VBA object model is stable across Windows and Mac.

What You'll Need

  • Excel for Windows or Excel for Mac (the VBA editor is available on both; on Mac, use Tools > Macro > Visual Basic Editor).
  • The Developer tab enabled, or the ability to press Alt+F11 (Windows) or Option+F11 (Mac) to open the editor directly.
  • A workbook with grouped or merged cells to test on — save a copy first, since macros that ungroup cannot be undone with Ctrl+Z after the macro finishes.
  • Macro security set to allow macros in the file, or the workbook saved as .xlsm. If macros are disabled, the code will not run at all.

Step-by-Step

  1. Make a backup copy. Save the workbook under a new name before running anything. VBA changes are not reversible with the normal Undo stack once the macro completes, so a copy is your only safety net.
  2. Open the VBA editor. Press Alt+F11 on Windows or Option+F11 on Mac. If nothing happens, enable the Developer tab via File > Options > Customize Ribbon and tick Developer, then click Visual Basic.
  3. Insert a module. In the editor, go to Insert > Module. This gives you a blank code window. Do not paste code into a sheet module unless you specifically want it tied to that sheet's events.
  4. Paste the basic ungroup macro. For outline groups on the current selection, use:
    Sub UngroupSelection()
        On Error Resume Next
        Selection.Ungroup
        On Error GoTo 0
    End Sub
    The On Error Resume Next line is deliberate: if the selection contains no grouped rows or columns, Excel raises a run-time error 1004. Suppressing it lets the macro finish quietly.
  5. Run it. Close the editor, select the rows or columns you want to ungroup on the worksheet, then press Alt+F8, choose UngroupSelection, and click Run. Each run removes one outline level, so a sheet grouped three levels deep needs three runs.
  6. Ungroup everything on a sheet at once. To strip all outline levels from the active worksheet, use:
    Sub UngroupAll()
        On Error Resume Next
        Cells.Ungroup
        On Error GoTo 0
    End Sub
    This clears every group on the active sheet but leaves other sheets untouched.
  7. Unmerge cells instead, if that is the real problem. If your "grouped" cells are actually merged, run:
    Sub UnmergeAll()
        On Error Resume Next
        Cells.UnMerge
        On Error GoTo 0
    End Sub
    Or target a specific area: Range("A1:D20").UnMerge. The value from the top-left cell of each merged block is kept; the other cells are emptied.
  8. Loop through every sheet in the workbook. To clean an entire file in one pass:
    Sub UngroupWorkbook()
        Dim ws As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            On Error Resume Next
            ws.Cells.Ungroup
            ws.Cells.UnMerge
            On Error GoTo 0
        Next ws
    End Sub
    This handles both outline groups and merged cells on every sheet, which is usually what people actually want when cleaning up a received file.
  9. Save as .xlsm. If you want to keep the macro, use File > Save As and choose Excel Macro-Enabled Workbook. Saving as .xlsx strips the code silently.

Common Mistakes

  • Using Ungroup on merged cells. This is the number one error. Merged cells need UnMerge; outline groups need Ungroup. If nothing happens, you picked the wrong method.
  • Forgetting that Ungroup removes only one level. A single call does not flatten a deeply nested outline. Loop it, or call Ungroup repeatedly until no error is raised.
  • Running on the wrong sheet. Selection.Ungroup acts on whatever is selected in the active sheet. If you switch sheets mid-macro, the selection may no longer be what you expect.
  • Leaving error handling off. Without On Error Resume Next, ungrouping a range with no groups stops the macro with error 1004 and any later steps never run.
  • Assuming UnMerge preserves all data. It does not. Only the top-left value survives each merged block; the rest is discarded, and there is no prompt.
  • Saving as .xlsx. The macro disappears without a warning dialog in many versions.

Tips

  • Check what you are dealing with first: select the cells and look at Home > Merge & Center. If the button is highlighted, the cells are merged, not grouped.
  • Use ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1 to collapse an outline to its top level without removing the grouping — useful when you want the structure kept but the detail hidden.
  • Clear outline entirely with ActiveSheet.ClearOutline. This removes all grouping levels and the margin buttons in one call, which is faster than looping Ungroup.
  • Assign the macro to a button (Developer > Insert > Button) so non-technical colleagues can clean up files without opening the editor.
  • Test on a copy with a small range before running a workbook-wide loop; a mistake on 40 sheets is tedious to reverse.

Troubleshooting

Nothing happens when I run the macro. The cells are almost certainly merged rather than grouped. Run the UnMerge version instead. If they are neither merged nor grouped, the macro has nothing to act on and will exit silently because of the error handler.

Run-time error 1004. This means the range has no groups to remove. Add On Error Resume Next around the call, or check the range first with If Selection.OutlineLevel > 1 Then.

The macro does nothing on some sheets. Protected sheets block structural changes. Unprotect them first (Review > Unprotect Sheet), or the macro will fail silently under the error handler. If you do not know the password, you cannot ungroup those sheets programmatically — ask the file's owner.

Data disappeared after unmerging. That is expected behaviour: only the top-left cell of each merged block keeps its value. Restore from your backup and, if needed, copy the values into a helper column before unmerging.

The macro is gone after saving. The file was saved as .xlsx. Re-add the code and save as .xlsm.

Macros are blocked entirely. Check File > Options > Trust Center > Trust Center Settings > Macro Settings. Enable macros for the session, or move the file to a trusted location. Do not lower security settings permanently for a one-off task.

Frequently Asked Questions

What is the difference between Ungroup and UnMerge in VBA?

Ungroup removes outline grouping (the +/- buttons created with Data > Group). UnMerge splits merged cells back into individual cells. They are unrelated features and neither affects the other.

Can I ungroup cells without a macro?

Yes. Select the rows or columns and use Data > Ungroup, or Home > Merge & Center to unmerge. The macro is only worth it when you have many sheets or a repetitive cleanup task.

Does Ungroup remove all levels at once?

No. Each call removes one outline level. To clear everything, call it repeatedly, loop until it errors, or use ClearOutline on the worksheet.

Why does my macro stop with error 1004?

The selected range contains no groups, so Excel has nothing to ungroup. Wrapping the call in On Error Resume Next lets the macro continue.

Can I undo a VBA ungroup?

Not with Ctrl+Z after the macro finishes. VBA clears the undo stack when it runs, so always work on a copy or save before running.

Will ungrouping affect formulas or formatting?

Outline grouping does not change cell contents or formatting. Unmerging keeps the top-left value and its formatting but empties the other cells in each merged block, which can break formulas that referenced those cells.