Looping a macro through all visible sheets?

GlennJ

Board Regular
Joined
Mar 26, 2018
Messages
59
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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,239
Office Version
  1. 365
Platform
  1. Windows
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
 

GlennJ

Board Regular
Joined
Mar 26, 2018
Messages
59
Office Version
  1. 2016
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,239
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

That image is not showing I14, so is not very helpful. In what way didn't the code work?
 

GlennJ

Board Regular
Joined
Mar 26, 2018
Messages
59
Office Version
  1. 2016
Platform
  1. Windows
Hi Rick,

I get this error

Screenshot 2021-06-05 142248.jpg
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,239
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

As you cannot have two sheets with the same name, what do you want to happen?
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,036
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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?
 

GlennJ

Board Regular
Joined
Mar 26, 2018
Messages
59
Office Version
  1. 2016
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,239
Office Version
  1. 365
Platform
  1. Windows
Yes, but what should happen if that sheet name is already being used?
 

Watch MrExcel Video

Forum statistics

Threads
1,132,899
Messages
5,655,861
Members
418,248
Latest member
JinxedCaspa

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
Top