VBA macro to Collapse/Expand all grouped rows/columns

imaguy77

New Member
Joined
Aug 26, 2008
Messages
21
Hi all,

I'd like to be able to run a macro which would collapse or expand all grouped rows and columns for all selected worksheets. When I say collapse or expand, i don't need any selection dialog box or anything...just the code which would accomplish it. Thanks!!
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787
The Outline.ShowLevels method allows you to show a specified number of
RowLevels and Column Levels.

To Collapse all Rows and Columns...
Code:
Sub Collapse_All()
    ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
End Sub
To Expand all Rows and Columns...
Code:
Sub Expand_All()
    ActiveSheet.Outline.ShowLevels RowLevels:=8, ColumnLevels:=8
End Sub
And anything in between....
Code:
Sub Collapse_Rows_to_Level2_Expand_All_Columns()
    ActiveSheet.Outline.ShowLevels RowLevels:=2, ColumnLevels:=8
End Sub
 

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787
I just noticed you want to apply this to all on selected worksheets.

I'd like to be able to run a macro which would collapse or expand all grouped rows and columns for all selected worksheets.
You can merge the code I posted with the method of iterating through
selected worksheets that Domenic provided you in your recent post.

The result for Expand_All would look like this.

Code:
Sub Expand_All()
    Dim sh As Object
    For Each sh In ActiveWindow.SelectedSheets
        If TypeName(sh) = "Worksheet" Then
            sh.Outline.ShowLevels RowLevels:=8, ColumnLevels:=8
        End If
    Next sh
    Set sh = Nothing
End Sub
You can adapt the other variations the same way.
 

jbaccash

New Member
Joined
Jul 6, 2012
Messages
3
This works great for expanding all rows/columns. I'm having some trouble searching online to find code to expand only one group of columns. I'm running Excel 2010. Googling around, I'm finding code such as:

Range("B:C").Columns.ShowDetail = True

Range("B:C").Columns.ShowLevels ColumnLevels:=2

I'm also guessing with code such as:

Range("B:C").Outline.ShowLevels ColumnLevels:=2

Columns("B:C").Columns.ShowLevels ColumnLevels:=2

I keep getting a "Runtime error 438: Object doesn't support this property or method".

Forgive me but I'm almost a total novice when it comes to writing code in VBA. My experience to date is recording clunky macros and then messing with the resulting code. Clicking on a particular "+" for a group doesn't generate any code when recording.

Any help is appreciated.
 

jbaccash

New Member
Joined
Jul 6, 2012
Messages
3
I also find it strange that

Rows(9).ShowDetail = True

works to expand a particular group of rows but

Columns(C).ShowDetail = True

does not work for columns.
 

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787
Hi and Welcome to the Board,

You could use either of these...

Code:
Columns("C").ShowDetail = True

Columns(3).ShowDetail = True
 

Watch MrExcel Video

Forum statistics

Threads
1,101,817
Messages
5,483,080
Members
407,378
Latest member
swanbrown

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top