How to run macro using workbook_open on multiple sheet

Bebek

New Member
Joined
Sep 17, 2021
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
Hello all. I'm new to the world of macros so I don't understand anything and I need help, Thank You.

I want to run my macro automatically when excel is opened, so I used Workbook_Open to solve it. But the problem is my macros just work for 1 out of 5 sheets. What I want is my macros will work 3 out of 5 sheets. Any help is greatly appreciated Thank You:biggrin:

Here are my macros


Sub Workbook_Open()
Dim sh As Worksheet

Set sh = Array("Sheet1", "Sheet2","Sheet3").Select

sh.Protect "123456", UserInterfaceOnly:=True
sh.EnableOutlining = True

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Would this work for you?

VBA Code:
Sub Workbook_Open()
   
    Dim sh As Worksheet
    Dim arr As Variant
    Dim elem As Variant
   
    arr = Array("Sheet1", "Sheet2", "Sheet3")
   
    For Each elem In arr

        Set sh = ThisWorkbook.Worksheets(elem)
     
        sh.Protect "123456", UserInterfaceOnly:=True
        sh.EnableOutlining = True

    Next elem

    set sh = Nothing

End Sub
 
Upvote 0
Solution
Thank You very much Mr. Gokhan for your support, it was really great answer. But when I run it out, error massage box "Run-time error 1004" appears.
 
Upvote 0
Which line is highlighted if you click debug?
Are the sheet names correct?
I didn't test the code inside for each...next loop (2 lines from your code) by the way.

I am not in front of a PC at this moment.

Edit: I wonder if procedure should start as "Private Sub" instead.
 
Upvote 0
OH, I'm really sorry Mr. Gokhan, the cause of the problem is, that I haven't unprotected the sheet before run the macro that you gave.

Thank you very much Mr. Gokhan for your support, I'm very greatful for your help. Much appreciated! Hope you have a great day Mr. Gokhan.

Warm Regards, Bebek from Indonesia:biggrin:
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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