Nested For Each Sheet Loop Issue

danbrawl93

New Member
Joined
Dec 15, 2016
Messages
17
Hey guys,

I am trying to run the following (simplified) code:

Code:
For Each sh In Worksheets
' I want to find if a particular sheet name exists
If sh.Name = "Desired Name" Then

    ' Now as a result I want to perform changes on specific sheets
    For Each sh In Worksheets
    If sh.name = "Example Name 1" Then
    ' Perform changes
    ElseIf sh.Name = "Example Name 2" Then
    ' Perform changes
    End If
    Next sh

End If
Next sh

The For variable (sh in this case) is being used more than once, so the code doesn't work, but I need to scan through the worksheets in the nested loop. Please can you suggest ways to work around the issue!

Thanks so much in advance,

Dan :)
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Can you explain why we need to run thru all the worksheets for each worksheet?
 
Upvote 0
Hey,

That would be great. But how do I use a different variable to represent worksheets? (i.e. I'm using "sh" to reference worksheets, what other variable name would be applicable?).

Thanks!
 
Upvote 0
Hey,

That would be great. But how do I use a different variable to represent worksheets? (i.e. I'm using "sh" to reference worksheets, what other variable name would be applicable?).

Thanks!
Instead of using sh for the inner loop use sh1, for example.

Don't forget that sh/sh1 is the means you have to step through the collection of worksheets and therefore reference the name of each worksheet.
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,010
Members
449,204
Latest member
tungnmqn90

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