VBA to test Outline levels

AllisterB

Board Regular
Joined
Feb 22, 2019
Messages
120
Office Version
  1. 365
Platform
  1. Windows
I am wanting to give the user the choice to expand the Outline Levels or not. I first want to check that the levels are not fully expanded already. However my code below errors on the first IF. Can someone help me with this?

Thank You

VBA Code:
Private Sub Worksheet_Activate()

'Anchors Freeze Frame
    ActiveWindow.FreezePanes = False

    Range("r_Anchor").Select
    
    ActiveWindow.FreezePanes = True
    'Protection to allow other macros to run on the sheet and to allow Filter and Grouping
 
    Me.Protect DrawingObjects:=True, Contents:=True, UserInterfaceOnly:=True, AllowFiltering:=True, AllowFormattingRows:=True, _
    AllowInsertingRows:=True, AllowFormattingCells:=True
Me.EnableOutlining = True

If Me.Outline.ShowLevels RowLevels:=4 and Me.Outline.ShowLevels ColumnLevels:=4 Then Exit Sub
If MsgBox("Do you want to Expand the  Rows and Columns", vbYesNo) = vbNo Then Exit Sub

'Expand Data Group Rows
  Me.Outline.ShowLevels RowLevels:=4, ColumnLevels:=2


End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hello,

think i may have found a workround, change the IF code line to

VBA Code:
If Columns("A:A").SpecialCells(xlCellTypeVisible).Count = Columns("A:A").Cells.Count Then Exit Sub

any use?
 
Upvote 0
Thanks for this

I tried but got a 1004 error.
VBA Code:
Private Sub Worksheet_Activate()

'Anchors Freeze Frame
    ActiveWindow.FreezePanes = False

    Range("r_Anchor").Select
    
    ActiveWindow.FreezePanes = True
    'Protection to allow other macros to run on the sheet and to allow Filter and Grouping
 
    Me.Protect DrawingObjects:=True, Contents:=True, UserInterfaceOnly:=True, AllowFiltering:=True, AllowFormattingRows:=True, _
    AllowInsertingRows:=True, AllowFormattingCells:=True
Me.EnableOutlining = True



If Columns("A:A").SpecialCells(xlCellTypeVisible).Count = Columns("A:A").Cells.Count Then Exit Sub
If MsgBox("Do you want to Expand the  Rows and Columns", vbYesNo) = vbNo Then Exit Sub

'Expand Data Group Rows
  Me.Outline.ShowLevels RowLevels:=4, ColumnLevels:=2


End Sub
 
Upvote 0
Hello,

at what line are you getting the error?

The code works for em, with two group of rows and one group of columns
 
Upvote 0

Forum statistics

Threads
1,214,556
Messages
6,120,190
Members
448,949
Latest member
keycalinc

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