Running macros on a protected workbook?

Mark McInerney

Active Member
Joined
Apr 4, 2012
Messages
262
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am trying to decipher how to run a macro when I have protected all the worksheets on my spreadsheet. I am not an experienced VBA person so appreciate any help on this.

My attempt (which is largely driven by using the 'record macro' option) is as follows:

Sub Split_Shift()
'
' Split_Shift Macro
'


'
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Unprotect Password:="W0UGHREA"
Next ws



Columns("E:O").Select
Selection.EntireColumn.Hidden = True
Columns("D:P").Select
Selection.EntireColumn.Hidden = False
Range("B13").Select

For Each ws In ThisWorkbook.Worksheets
ws.Protect Password:="W0UGHREA"
Next ws
ActiveSheet.Select
End Sub

The problem I have is that I keep ending up on a different worksheet and not the one that I ran the macro from?

Any help appreciate
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Save a reference to the active sheet before your code changes the active sheet so that you can put it back just before your code finishes.

Changes / additions to your code in blue:

Code:
Sub Split_Shift()
'
' Split_Shift Macro
'
'
Dim ws As Worksheet
[COLOR=#0000ff]Dim oActive As Worksheet

Set oActive = ActiveSheet[/COLOR]

For Each ws In ThisWorkbook.Worksheets
ws.Unprotect Password:="W0UGHREA"
Next ws

Columns("E:O").Select
Selection.EntireColumn.Hidden = True
Columns("D:P").Select
Selection.EntireColumn.Hidden = False
Range("B13").Select

For Each ws In ThisWorkbook.Worksheets
ws.Protect Password:="W0UGHREA"
Next ws

[COLOR=#0000ff]oActive.Activate[/COLOR]

ActiveSheet.Select

End Sub


Gary
 
Upvote 0
Hi Gary,

Thanks very much for your help - really appreciated. Many Thanks. Mind Yourself - Mark.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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