Mocro to Loop through Workbook and Rename Tabs

KeithG

New Member
Joined
Aug 17, 2007
Messages
15
Each month I get a workbook with 30+ worksheets (Sheet1, Sheet2, Sheet3....Sheet30+). I was wondering if there is a macro that could loop through all 30 worksheets and rename them to the value in cell "U9". The value is U9 is unique for each worksheet. I tried searching the board but could not find something with my specific issues:

I don't create the worksheets in the workbook. They are already created by the time I receive it.

Sometimes the value in U9 contains invalid characters..\ / *
Sometimes the value in U9 contains more than 31 characters

I am okay with something that will skip the worksheets that contain invalid characters. I can do them manually. they are only small % of total worksheets. I would love something that would loop through each worksheet ant rename it to the value in cell "U9".

Any help???
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
This should do it (untested):
Code:
Sub blah()
For Each sht In ActiveWorkbook.Sheets
    sht.Name = Replace(Replace(Replace(Replace(Replace(Replace(Replace(sht.Range("U9").Value, ":", ""), "/", ""), "\", ""), "?", ""), "*", ""), "[", ""), "]", "")
Next sht
End Sub
 
Last edited:
Upvote 0
Try for that long middle line:
Code:
    sht.Name = Left(Replace(Replace(Replace(Replace(Replace(Replace(Replace(sht.Range("U9").Value, ":", ""), "/", ""), "\", ""), "?", ""), "*", ""), "[", ""), "]", ""), 31)
 
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,214
Members
452,895
Latest member
BILLING GUY

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