Delete Only Grouped Columns or Rows

Learn VBA Macros From Microsoft Excel Code Vault Snippets

What This VBA Code Does

This VBA code will loop through the columns/rows on your active worksheet and delete the ones that are grouped together.  You could easily modify this snippet to only format grouped columns or rows too, if that is what you need.

Delete Only Grouped Columns:

Sub Delete_Grouped_Columns()

'PURPOSE: Delete Only Columns that are Grouped within the ActiveSheet
'SOURCE: www.TheSpreadsheetGuru.com

Dim x As Long
Dim LastColumn As Long
Dim rng As Range

'Retrieve Range of Cells with Data
  Set rng = ActiveSheet.UsedRange

'Determine Last Column in Data Set
  LastColumn = rng.Columns.Count

'Loop Through Columns and Delete Columns that are Grouped
    For x = 1 To LastColumn
        If rng.Columns(x).OutlineLevel > 1 Then
            rng.Columns(x).EntireColumn.Delete
            x = x - 1
        End If
    Next x

End Sub

Delete Only Grouped Rows:

Sub Delete_Grouped_Rows()

'PURPOSE: Delete Only Rows that are Grouped within the ActiveSheet
'SOURCE: www.TheSpreadsheetGuru.com

Dim x As Long
Dim LastRow As Long
Dim rng As Range

'Retrieve Range of Cells with Data
  Set rng = ActiveSheet.UsedRange

'Determine Last Row in Data Set
  LastRow = rng.Rows.Count

'Loop Through Rows and Delete Rows that are Grouped
    For x = 1 To LastRow
        If rng.Rows(x).OutlineLevel > 1 Then
            rng.Rows(x).EntireRow.Delete
            x = x - 1
        End If
    Next x

End Sub

How Do I Modify This To Fit My Specific Needs?

Chances are this post did not give you the exact answer you were looking for. We all have different situations and it's impossible to account for every particular need one might have. That's why I want to share with you: My Guide to Getting the Solution to your Problems FAST! In this article, I explain the best strategies I have come up with over the years to getting quick answers to complex problems in Excel, PowerPoint, VBA, you name it

I highly recommend that you check this guide out before asking me or anyone else in the comments section to solve your specific problem. I can guarantee 9 times out of 10, one of my strategies will get you the answer(s) you are needing faster than it will take me to get back to you with a possible solution. I try my best to help everyone out, but sometimes I don't have time to fit everyone's questions in (there never seem to be quite enough hours in the day!).

I wish you the best of luck and I hope this tutorial gets you heading in the right direction!

Chris "Macro" Newman :)