Rename a sheet based on data in a cell

Marc_w90

Board Regular
Joined
Jul 11, 2014
Messages
74
Office Version
  1. 365
Platform
  1. Windows
I'm trying to relearn some VBA and hoping someone can help me out.
I'm trying to make a macro that will do the following:

I want the macro to change the old sheet names in column A to the new sheet names on the column B
Column A has a list of current sheet names (length of column unknown but maximum of 100 lines)
Column B has a list of new sheet names
If Column B is blank, leave the sheet name as it is.

Thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Like this:
Assume list of sheets name start from A1, all the way down:
VBA Code:
Option Explicit
Sub rename()
Dim lr&, cell As Range
lr = Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In Range("A1:A" & lr)
    If Evaluate("=ISREF(" & cell.Value & "!A1)") Then
        Sheets(cell.Value).Name = IIf(IsEmpty(cell.Offset(, 1)), cell.Value, cell.Offset(, 1).Value)
    End If
Next
End Sub
 
Upvote 0
Hi,
Thanks for this.

Having a few issues and not really sure why.

I have the list of 33 sheet names (starting in A1 like you suggested) and I have TEST1, TEST2 etc running along side in column B.

For some reason, when I run the macro above, it changes lines 24-27 but non of the others.
I know the names of the sheets are correct because I used another macro to extract them.
Can you suggest any reasons why this might happen?

Thanks
 
Upvote 0
Hi,
Thanks for this.

Having a few issues and not really sure why.

I have the list of 33 sheet names (starting in A1 like you suggested) and I have TEST1, TEST2 etc running along side in column B.

For some reason, when I run the macro above, it changes lines 24-27 but non of the others.
I know the names of the sheets are correct because I used another macro to extract them.
Can you suggest any reasons why this might happen?

Thanks
Partially solved my own problem, for some reason the code above doesn't like Sheet names with any space in it anywhere.

Is there a way we can allow for spaces in sheet names?

Thanks
 
Upvote 0
You would need to use
VBA Code:
If Evaluate("=ISREF('" & cell.Value & "'!A1)") Then
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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