Looping a macro through all visible sheets?

GlennJ

Board Regular
Joined
Mar 26, 2018
Messages
63
Office Version
  1. 2016
Platform
  1. Windows
I have a macro that does exactly what I need. I just need it to do the same thing to all sheets. Any help would be appreciated.

VBA Code:
Sub Name_Sheet()

If Range("I14").Value = "" Then
    'Do nothing
Else
    ActiveSheet.Name = Range("I14").Value
End If

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How about
VBA Code:
Sub Name_Sheet()
   Dim Ws As Worksheet
   
   For Each Ws In Worksheets
      If Ws.Range("I14").Value <> "" Then Ws.Name = Ws.Range("I14").Value
   Next Ws
End Sub
 
Upvote 0
How about
VBA Code:
Sub Name_Sheet()
   Dim Ws As Worksheet
  
   For Each Ws In Worksheets
      If Ws.Range("I14").Value <> "" Then Ws.Name = Ws.Range("I14").Value
   Next Ws
End Sub
Thanks for getting back so quickly.

I think I need to explain better. The worksheet is already named Jane Doe, but if there is a Pickup By: Person, I need to change the sheet name to their name (Dave Smith). If there isn't a pickup person don't change the name.

Screenshot 2021-06-05 140922.jpg
 
Upvote 0
That image is not showing I14, so is not very helpful. In what way didn't the code work?
 
Upvote 0
As you cannot have two sheets with the same name, what do you want to happen?
 
Upvote 0
The only way that would have happened is if the same name was put in cell I14 on more than one sheet. What is supposed to happen in that case?
 
Upvote 0
If there's a pickup person, change the sheet name to the pickup person (Dave Smith). If there isn't a pickup person don't change the sheet name as it is already (Jane Doe). And repeat for all Sheets
Thanks for trying to work with me.

jane.jpg
 
Upvote 0
Yes, but what should happen if that sheet name is already being used?
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,755
Members
448,989
Latest member
mariah3

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