Rename Sheets based on cell values from the first sheet in the workbook.

iscon

New Member
Joined
Mar 6, 2019
Messages
2
Hi,

I'm very new to VBA. I have a problem figuring out how to do a macro. I want to rename sheets in my workbook based on values from specific cells on the first page (the front page). Is there someone who can maybe give me some advice.

For example I want to put in values in G3, G4, G5, that will automatically rename sheet 2,3,4 etc. Is this even possible?

Thanks,

V
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hey, something like this would work for your example given:

Code:
Sub ChangeSheetNames()
'Note 31 chars is the max
    Sheets(2).Name = Left(Sheets(1).Range("G3").Value, 31)
    Sheets(3).Name = Left(Sheets(1).Range("G4").Value, 31)
    Sheets(4).Name = Left(Sheets(1).Range("G5").Value, 31)
End Sub

Tabs have a maximum of 31 characters so hence the Left function to take the first 31 characters.
 
Last edited:
Upvote 0
Welcome to the Board!

Here is some code that uses a loop, so it will work on any size range without having to add more rows to your code. I added lots of comments to explain what each step is doing. You may want to add some error handling to check for invalid names or sheet numbers.
Code:
Sub MyRenameSheets()

    Dim rng As Range
    Dim cell As Range
    Dim shtnum As Long
        
'   Set range that has names to rename sheets
    Set rng = Sheets("Sheet1").Range("G3:G5")
    
'   Set index of first sheet to rename
    shtnum = 2
    
'   Loop through range
    For Each cell In rng
'       Rename sheet
        Sheets(shtnum).Name = cell.Value
'       Increment sheet index
        shtnum = shtnum + 1
    Next cell
    
End Sub
 
Last edited:
Upvote 0
Here is a variation with some simple error handling that just lets you know what sheet/name it errored out on.
Code:
Sub MyRenameSheets()

    Dim rng As Range
    Dim cell As Range
    Dim shtnum As Long
        
'   Set range that has names to rename sheets
    Set rng = Sheets("Sheet1").Range("G3:G6")
    
'   Set index of first sheet to rename
    shtnum = 2
    
    On Error GoTo err_chk
'   Loop through range
    For Each cell In rng
'       Rename sheet
        Sheets(shtnum).Name = cell.Value
'       Increment sheet index
        shtnum = shtnum + 1
    Next cell
    On Error GoTo 0
    
    Exit Sub
    
err_chk:
    MsgBox "Error trying to assign name " & cell.Value & " to sheet " & shtnum
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,927
Members
448,533
Latest member
thietbibeboiwasaco

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