Looping through worksheets

OfficeUser

Well-known Member
Joined
Feb 4, 2010
Messages
544
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am using this to try and loop through all worksheets and change one cell. Problem is it only will alter the active sheet from which I was on when I trigger the macro. Any suggestions?

Code:
Sub ChangeAll()
    Dim Current As Worksheet
         For Each Current In Worksheets
            ActiveSheet.Unprotect Password:="password"
            Range("B6").Value = "Off"
            ActiveSheet.Protect Password:="password"
         Next
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
In that structure, Current becomes a worksheet object representing each sheet during the loop.
So use that in the code.

Change each instance of ActiveSheet to Current
And add current to the range..
Current.Range("B6").Value = "Off"
 
Upvote 0
Try:
Code:
Sub ChangeAll()

Dim Current As Worksheet
    
With Current
         For Each Current In Worksheets
            .Unprotect Password:="password"
            .Range("B6").Value = "Off"
            .Protect Password:="password"
         Next
End With
End Sub
 
Upvote 0
Try:
Code:
Sub ChangeAll()
 
Dim Current As Worksheet
 
With Current
         For Each Current In Worksheets
            .Unprotect Password:="password"
            .Range("B6").Value = "Off"
            .Protect Password:="password"
         Next
End With
End Sub

I tried this and received an error
Runtime error 91: Object variable or With block variable not set.
 
Upvote 0
I figured it out since. I followed jonmo1's instructions and all worked out well. Thanks to you both for the help.
 
Upvote 0
.......just for the record - in case other users use this thread........

I should have put the With statement inside the loop......

This now works:
Code:
Sub ChangeAll()

Dim Current As Worksheet
 
     For Each Current In Worksheets
        With Current
            .Unprotect Password:="password"
            .Range("B6").Value = "Off"
            .Protect Password:="password"
        End With
     Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

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