To ungroup cells (rows or columns) with Excel VBA, you use the Ungroup method on the Range object. The simplest working code is Selection.Ungroup or ActiveSheet.Rows("2:5").Ungroup. This removes the outline grouping (the plus/minus buttons and the bracket lines) from the selected range. You can ungroup rows, columns, or both, and you can control whether Excel ungroups just the selected range or the entire sheet. This guide covers the exact code, how to run it, common pitfalls, and fixes for when it doesn't work.
What You'll Need
- A workbook that contains grouped rows or columns (grouping appears as a bracket and a minus sign on the left side for rows or at the top for columns).
- Access to the Visual Basic Editor (press Alt+F11 in Excel).
- Basic familiarity with inserting a module and running a macro (if not, steps 1–3 below cover it).
Step-by-Step
- Open the Visual Basic Editor – In Excel, press Alt+F11 (or go to the Developer tab and click Visual Basic). If you don't see the Developer tab, right-click any ribbon area, choose Customize the Ribbon, and check Developer.
- Insert a module – In the VBA editor, go to Insert > Module. This creates a new code window where you'll paste the macro.
- Write the macro – Copy one of the following code examples into the module. The first is the most common: it ungroups the currently selected cells.
Sub UngroupSelected()
Selection.Ungroup
End Sub
If you want to ungroup specific rows or columns without selecting them first, use a direct reference. For example, to ungroup rows 2 through 5 on the active sheet:
Sub UngroupRows()
ActiveSheet.Rows("2:5").Ungroup
End Sub
To ungroup all rows and columns on the entire sheet at once, use the Cells reference with the Position argument set to xlRows and xlColumns separately:
Sub UngroupAll()
ActiveSheet.Cells.Ungroup Position:=xlRows
ActiveSheet.Cells.Ungroup Position:=xlColumns
End Sub
Note: The Position argument is optional. If you omit it, Excel ungroups both rows and columns in the selected range. But when you use Cells (the entire sheet), it's safer to specify the position to avoid an error if the sheet has no groups of one type.
- Run the macro – With the cursor anywhere inside the macro code, press F5 (or go to Run > Run Sub/UserForm). If you used
Selection, first select the grouped cells in the worksheet before running. - Check the result – The brackets and minus signs should disappear. If they don't, the range you selected might not have been grouped, or the outline might be hidden (see Troubleshooting).
Common Mistakes
- Using
Ungroupon a range that isn't grouped – If you try to ungroup cells that have no grouping, Excel may show a prompt asking whether you want to ungroup the entire sheet, or it may do nothing. Always check that the range actually has an outline. - Forgetting to specify rows vs. columns – When you use
Selection.Ungroupwithout thePositionargument, it ungroups both rows and columns in that selection. That's usually fine, but if you only want rows, you needSelection.Ungroup Position:=xlRows. - Ungrouping the whole sheet unintentionally – If you select a single cell and run
Selection.Ungroup, Excel might ungroup the entire sheet because it interprets the selection as being inside a grouped range. To avoid this, select the exact grouped rows or columns first. - Using
Rowsvs.EntireRowincorrectly –Rows("2:5")refers to rows 2 through 5. If you useRange("A2:A5").Ungroup, it will ungroup the columns that contain those cells (columns A), not the rows. To ungroup rows from a range, useRange("A2:A5").EntireRow.Ungroup. - Not clearing the outline after ungrouping – Sometimes the plus/minus buttons disappear but the outline symbols remain (the small numbered buttons at the top-left). To remove those, you need to clear the outline entirely. Use
ActiveSheet.ClearOutlineafter ungrouping, but note that this removes all grouping on the sheet.
Tips
- Use
ClearOutlinefor a clean slate – If you want to remove all grouping from a sheet,ActiveSheet.ClearOutlineis the fastest method. It removes every group (rows and columns) in one line. However, it also removes any custom outline levels you've set. - Check if a range is grouped before ungrouping – You can test if a range has an outline using
If ActiveSheet.Outline.SummaryRow = xlAbove Thenor by checking theRange.Rows.GroupLevelproperty. For example,If ActiveSheet.Rows("2").GroupLevel > 0 Thentells you if row 2 is grouped. This prevents errors. - Ungroup only visible cells – If you have filtered data, ungrouping might affect hidden rows. Use
Selection.SpecialCells(xlCellTypeVisible).Ungroupto limit the action to visible cells, though this can be unpredictable with outlines. - Record a macro to learn the exact syntax – If you're unsure about the exact method for a specific scenario, turn on the macro recorder (Developer tab > Record Macro), manually ungroup the cells, then stop recording and view the generated code. It will show the precise syntax Excel uses for that action.
- Use
Application.ScreenUpdating = Falsefor speed – If you're ungrouping many ranges in a loop, disabling screen updating makes the macro run faster and avoids flicker. Remember to set it back toTrueat the end.
Troubleshooting
If the macro runs but nothing happens:
- Check that the range you selected actually has grouping. Look for the bracket lines and minus signs on the row numbers or column letters. If they're not visible, the sheet might have the outline symbols hidden. Go to File > Options > Advanced, scroll to the Display options for this worksheet, and check “Show outline symbols if an outline is applied.”
- If you used
Selection.Ungroupand the selection is a single cell, Excel might not ungroup anything because it doesn't know which group you mean. Select the entire grouped rows or columns (click the row numbers or column letters) before running.
If you get an error like “Run-time error 1004: Method 'Ungroup' of object 'Range' failed”:
- This usually means the range you're trying to ungroup is not part of a group. Double-check that the range is actually grouped. If you're sure it is, try using
ActiveSheet.Cells.Ungroupto ungroup everything, then re-group only what you need. - Another cause is trying to ungroup a range that spans both grouped and ungrouped rows. Select only the grouped rows, not the entire sheet.
If the plus/minus buttons disappear but the outline numbers (1,2,3) remain:
- That means the group was removed but the outline level structure is still there. Run
ActiveSheet.ClearOutlineto fully remove the outline. Be aware this clears all grouping on the sheet.
If the macro works but only ungroups rows, not columns (or vice versa):
- You need to specify the
Positionargument. UseSelection.Ungroup Position:=xlColumnsto ungroup columns, orPosition:=xlRowsfor rows. If you omit it, Excel ungroups both, but if you usedCells.Ungroupit might only do one depending on the outline settings.
If the macro errors on a protected sheet:
- Ungrouping is a structural change. If the worksheet is protected, unprotect it first with
ActiveSheet.Unprotect(you may need a password) before running the ungroup code, then re-protect if needed.
Frequently Asked Questions
Can I ungroup only rows or only columns with a single line of code?
Yes. Use Selection.Ungroup Position:=xlRows for rows only, or Position:=xlColumns for columns only. If you omit the Position argument, Excel ungroups both.
What's the difference between Ungroup and ClearOutline?
Ungroup removes the grouping from a specific range, but it may leave the outline structure (the level numbers) intact. ClearOutline removes all grouping and outline structure from the entire worksheet in one go.
How do I ungroup all rows and columns on a sheet at once?
Use ActiveSheet.Cells.Ungroup without a Position argument. This ungroups both rows and columns across the entire sheet. If you only want rows, use ActiveSheet.Cells.Ungroup Position:=xlRows.
Why does my macro ask “Do you want to ungroup the entire sheet?”
That prompt appears when you try to ungroup a range that is not part of a group, or when the selection is ambiguous (like a single cell inside a grouped area). Excel is asking if you want to ungroup the whole sheet instead. To avoid the prompt, select the exact grouped rows or columns before running the macro.
Can I undo an ungroup operation in VBA?
Yes, you can press Ctrl+Z immediately after running the macro, just like any other Excel action. However, if the macro makes multiple changes, undo may only revert the last one. To be safe, save a copy of the workbook before running the macro.
Does ungrouping affect formulas or data?
No. Ungrouping only changes the outline display; it does not alter any cell values, formulas, formatting, or data. It only removes the grouping brackets and the collapse/expand buttons.