Expand / Collapse Row and Column Groupings using VBA

imtryinhard

New Member
Joined
Nov 14, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a personal macro script to Expand (Show Detail) or Collapse (Hide detail) of a very large (300R x 200C worksheet) that i frequently toggle between a summary view and full model view so i don't have to keep using my mouse click on the little 1-2-3 groups in the upper left with my mouse.

I want to have it so that if the outline is expanded (=2) then collapse it, adn if it's already collapsed, expand it.

But this code isn't working

VBA Code:
Sub CollapseExpando()

If ActiveSheet.ShowLevels.rowlevels = 2 Then
    ActiveSheet.Outline.ShowLevels rowlevels:=1
 
Else if ActiveSheet.ShowLevels.rowlevels = 1 Then
    ActiveSheet.Outline.ShowLevels rowlevels:=2
 
End If

End Sub

Help would be appreciated...thanks!
 
Last edited by a moderator:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I should have mentioned that i'm getting a runtime error '438' saying Object doesn't support this property or method
 
Upvote 0
Welcome to the MrExcel board!

Try this with a copy of your workbook.
The first time that you run the code after opening the workbook, level 2 should be shown. So, if it was already level 2 you will see no change. However, while the workbook is open, each running of the code should toggle the levels.

VBA Code:
Sub CollapseExpando_v2()
  Static myLevel As Long
  
  If myLevel = 0 Then myLevel = 1
  myLevel = 3 - myLevel
  ActiveSheet.Outline.ShowLevels rowlevels:=myLevel, columnlevels:=myLevel
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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