Apply macro to *all* sheets

Caitlin535

New Member
Joined
Jan 8, 2016
Messages
21
Hello -
I have an Excel system that i use to track timekeeping with my employees. There is a row for each day, and in one cell they select "Arrived" from a drop down menu, enter a pin number, and then it automatically timestamps the date/time in another column. I have a macro that i use to set the pin number for the protected range on each sheet (see below). It used to work such that it would set the pin that I specified for *all* the worksheets in the file, but now it only sets it for the active worksheet. I feel like I'm missing something fairly simple - how can I get it to operate for *all* the worksheets in the file?

VBA Code:
Sub EditRange()
Dim Sh As Worksheet
Dim Rng As Range
Dim i As Integer
Set Sh = Application.ActiveSheet
On Error Resume Next
' Unprotect worksheet.
Sh.Unprotect
Set Rng = Sh.Range("$C$5:$F$18, $M$5:$N$18, $P$5:$Q$18")
' Delete all current protection ranges

'MsgBox ws.Protection.AllowEditRanges.Count

For i = 1 To Sh.Protection.AllowEditRanges.Count
Sh.Protection.AllowEditRanges(i).Delete

Next


' Establish a range that can allow edits
' on the protected worksheet.
Sh.Protection.AllowEditRanges.Add Title:="PINRange", Range:=Rng, Password:="a5678"

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
VBA Code:
Set Sh = Application.ActiveSheet

Your macro code is referencing the 'ActiveSheet' only. You'll need to edit your code so it references all sheets in workbook.
 
Upvote 0
I'm embarrassed to admit it, but I've never actually done that; all my macros usually refer only to the Active sheet. Could you tell me the language I would use to have the macro reference all the sheets in the workbook?
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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