VBA Button to Expand All / Collapse all groupings

zach9208

Board Regular
Joined
Dec 15, 2015
Messages
117
I need a VBA Button that will toggle between "Expand All" and "Callapse All" for the groupings on a given worksheet. Any help would be appreciated. Thanks!!!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You can apply any of these short macros to your needs.


Code:
Sub ExpandAllColumns()    
  ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=8
    
End Sub


Sub CollapseAllColumns()
    
  ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
    
End Sub


Sub CollapseAllRows()
    
  ActiveSheet.Outline.ShowLevels RowLevels:=1
    
End Sub




Sub ExpandAllRows()
    
  ActiveSheet.Outline.ShowLevels RowLevels:=8
    
End Sub
 
Upvote 0
I am looking for a code that I can put under one button. The first time I hit the button it should show. The second time I hit the button it should hide. Is this even possible?
 
Upvote 0
You can replace the column A with any column that would be hidden under your groups

Code:
Sub ToggleGroupExpand()  
If Range("A:A").EntireColumn.Hidden = True Then
    ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=8
  Else
    ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
  End If
End Sub
 
Last edited:
Upvote 0
This would only test one column. IF you wanted to include several columns in the mix:

Code:
Sub ToggleGroupExpand()  
If Range("A:A").EntireColumn.Hidden = True Or _
    Range("H:J").EntireColumn.Hidden = True Or _
    Range("M:N").EntireColumn.Hidden = True Then
    ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=8
  Else
    ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
  End If
End Sub
 
Upvote 0
Ended up figuring it out using some of your code. I ended breaking it up into a total of 3 subs. The first two subs collapse and expand and the last sub allows me to create 1 button to collapse/expand. Thanks all for the suggestions!

Code:
Sub CollapseAll()
  Sheet2.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
End Sub
Sub ExpandAll()
  Sheet2.Outline.ShowLevels RowLevels:=8, ColumnLevels:=8
End Sub


Sub ShowHideColumnsRows()
If ActiveSheet.Shapes("Button 3").TextFrame.Characters.Text = "Hide Groupings" Then
    ActiveSheet.Shapes("Button 3").TextFrame.Characters.Text = "Show Groupings"
    Call CollapseAll
Else
    ActiveSheet.Shapes("Button 3").TextFrame.Characters.Text = "Hide Groupings"
    Call ExpandAll
    ActiveSheet.Range("A1").Select
End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,859
Members
449,194
Latest member
HellScout

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top