Applying code to all worksheets.

OfficeUser

Well-known Member
Joined
Feb 4, 2010
Messages
544
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to do loop through all sheets and change a value but it only affects the sheet for which I am on when I run the code.

Code:
Sub ChangeAllWorksheets()
Dim ws As Worksheet
    For Each ws In Worksheets
     Range("A3").Value = "Yes"
    Next ws
End Sub

Any suggestions on what is missing, causing it to not work as planned? Thanks.
 
So this should work

Code:
Sub ChangeAllWorksheets()
Dim ws As Worksheet
    For Each ws In Worksheets
     ws.Shapes("lblGreen").Visible = False
    Next ws
End Sub
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I have to agree, but unfortunately it does not. I checked the name in the name box and it is lblGreen. I am unsure what the issue is.
 
Upvote 0
It works for me. Maybe there is a sheet where the name is " lblGreen" or "lblGreen "
 
Upvote 0
I just double checked. I am testing using 2 sheets, one is a mere copy of the other.
 
Upvote 0
Created a blank sheet, created lblGreen, copied sheet, used code and it worked. Time to go back to my sheet and investigate.
 
Upvote 0
You should be able to avoid the error like this (tested)

Code:
Sub ChangeAllWorksheets()
Dim ws As Worksheet, shp As Shape
    For Each ws In Worksheets
        For Each shp In ws.Shapes
            If shp.Name = "lblGreen" Then shp.Visible = False
        Next shp
    Next ws
End Sub
 
Upvote 0
Well I notice that when I get the 'Specified Item was not found error' and I close the error, lblGreen does disappear. So it is "working", just not properly.
 
Upvote 0
Do they disappear if you do this (not a recommended style of coding)

Code:
Sub ChangeAllWorksheets()
Dim ws As Worksheet
    For Each ws In Worksheets
        On Error Resume Next
        ws.Shapes("lblGreen").Visible = False
        On Error GoTo 0
    Next ws
End Sub
 
Upvote 0
You should be able to avoid the error like this (tested)

Code:
Sub ChangeAllWorksheets()
Dim ws As Worksheet, shp As Shape
    For Each ws In Worksheets
        For Each shp In ws.Shapes
            If shp.Name = "lblGreen" Then shp.Visible = False
        Next shp
    Next ws
End Sub

This code is working properly. I wish I could figure out why it would not work the other way.
 
Upvote 0

Forum statistics

Threads
1,224,575
Messages
6,179,637
Members
452,934
Latest member
Jdsonne31

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