Running 2 Macros sequentially when opening a sheet

dids86

New Member
Joined
Aug 15, 2024
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'd like 2 macros to automatically run when opening my excel sheet. At the moment I have to click 2 buttons seperately to enact this.

The first button is

Public Sub EnableOutliningWithProtection_AllSheets()
'PURPOSE: Allow Outline functionality during Protection in all Sheets
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim sht As Worksheet

'Loop through each Worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
'Unprotect Sheet
ActiveSheet.Unprotect ""

'Enable Group Collapse/Expand Capabilities
ActiveSheet.EnableOutlining = True
ActiveSheet.EnableAutoFilter = True

Next sht

End Sub

The second button is

Sub TextBox1_Click()

For a = 4 To 1500

If Worksheets("Salaries").Cells(a, 16).Value = "N" Then

Worksheets("Salaries").Rows(a).Hidden = True

End If

Next

End Sub



If I create the following

Public Sub Workbook_Open()

Call EnableOutliningWithProtection_AllSheets
Call TextBox1_Click


End Sub

Under the ThisWorkbook, it works, but only if the file is opened with the Salaries tab being the first tab that opens. I'd like a different tab to open initially.


With the tab I want to open I get the following error message

'Unable to set the hidden property of the range class'

And if I click debug it highlights the below

Worksheets("Salaries").Rows(a).Hidden = True

Do you know if it is possible for this to work with a different tab opening originally?

Thanks
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I am a bit unclear on what you are trying to do. The "EnableOutliningWithProtection_AllSheets" indicates that is meant to operate on every sheet in the workbook but it only operates on the activesheet. When Salaries is not the ActiveSheet it stays protected and that is what is causing your second macro to fail.
The below will unprotect all the sheets.
The EnableOutlining goes hand in hand with turning the protection back on, but I can't see where you are doing that.

VBA Code:
Public Sub EnableOutliningWithProtection_AllSheets()
'PURPOSE: Allow Outline functionality during Protection in all Sheets
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim sht As Worksheet

'Loop through each Worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
    'Unprotect Sheet
    sht.Unprotect ""
    
    'Enable Group Collapse/Expand Capabilities
    sht.EnableOutlining = True
    sht.EnableAutoFilter = True

Next sht

End Sub
 
Upvote 0
Thanks. Happy for the protection to be removed from all sheets. I applied your update and it gives me an error message of:

The password you supplied is not correct. Verify that the CAPS LOCK key is off and be sure to use the correct capitalization

If I hit debug it highlights

sht.Unprotect ""

Thanks

Tom
 
Upvote 0
I have used your original code.
If you don't have a password you can just use:
VBA Code:
sht.Unprotect

If you do have a password you need to provide the password eg
change the 123 to your password.
Rich (BB code):
sht.Unprotect Password:="123"
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,716
Members
452,995
Latest member
isldboy

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