For Each, Next ws not looping, possible range reference issue

Nic2

New Member
Joined
Sep 14, 2020
Messages
5
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello, I have this macro to unlock and highlight cells in a range that should apply to the same range in each sheet of the workbook. It works great on the active sheet but it isn't looping. I'm guessing it's an issue with the range reference from sheet to sheet but I can't seem to get it, any help is very much appreciated. Thank you

Sub Unlock_and_Highlight()
Dim Pd5 As Range
Dim ws As Worksheet
Set Pd5 = Range("N3:N8,N11:N20")
For Each ws In ThisWorkbook.Worksheets
If ws.Range("N2") = "Yes" Then Pd5.Locked = False
If ws.Range("N2") = "Yes" Then Pd5.Interior.Color = RGB(220, 220, 220)
Next ws
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Does it work if you move
VBA Code:
Set Pd5 = Range("N3:N8,N11:N20")
inside the loop?
VBA Code:
Sub Unlock_and_Highlight()
Dim Pd5 As Range
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
  Set Pd5 = ws.Range("N3:N8,N11:N20")
  If ws.Range("N2") = "Yes" Then Pd5.Locked = False
  If ws.Range("N2") = "Yes" Then Pd5.Interior.Color = RGB(220, 220, 220)
Next ws
End Sub
 
Upvote 0
Solution
Your code is looping, but Pd5 is constrained to the activesheet. Try this:
VBA Code:
Sub Unlock_and_Highlight()
Dim Pd5 As Range
Dim ws As Worksheet
Set Pd5 = Range("N3:N8,N11:N20")
For Each ws In ThisWorkbook.Worksheets
Set Pd5 = ws.Range("N3:N8,N11:N20")
If ws.Range("N2") = "Yes" Then Pd5.Locked = False
If ws.Range("N2") = "Yes" Then Pd5.Interior.Color = RGB(220, 220, 220)
Set Pd5 = Nothing
Next ws
End Sub
 
Upvote 0
Does it work if you move
VBA Code:
Set Pd5 = Range("N3:N8,N11:N20")
inside the loop?
VBA Code:
Sub Unlock_and_Highlight()
Dim Pd5 As Range
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
  Set Pd5 = ws.Range("N3:N8,N11:N20")
  If ws.Range("N2") = "Yes" Then Pd5.Locked = False
  If ws.Range("N2") = "Yes" Then Pd5.Interior.Color = RGB(220, 220, 220)
Next ws
End Sub
Thanks Mark that worked great!
 
Upvote 0
Your code is looping, but Pd5 is constrained to the activesheet. Try this:
VBA Code:
Sub Unlock_and_Highlight()
Dim Pd5 As Range
Dim ws As Worksheet
Set Pd5 = Range("N3:N8,N11:N20")
For Each ws In ThisWorkbook.Worksheets
Set Pd5 = ws.Range("N3:N8,N11:N20")
If ws.Range("N2") = "Yes" Then Pd5.Locked = False
If ws.Range("N2") = "Yes" Then Pd5.Interior.Color = RGB(220, 220, 220)
Set Pd5 = Nothing
Next ws
End Sub
Hi JoeMo thanks, this worked great also. So I see the difference between this and the code from Mark858 is the "Set PD5 = Nothing" line at the end, what does that do?
 
Upvote 0
Hi JoeMo thanks, this worked great also. So I see the difference between this and the code from Mark858 is the "Set PD5 = Nothing" line at the end, what does that do?
In essence it removes the current setting for the range object Pd5 for the worksheet ws.Name before moving to the next worksheet. It's not absolutely needed, but some regard setting range objects to Nothing once they are no longer required as good coding practice. If you comment out that line you will see that it makes no difference.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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