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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,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