VBA to make Sheet Name = Cell Value

JDSOuth49

New Member
Joined
Feb 16, 2024
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

I am tiring to rename a sheet tab based on a specific cell.

I have attempted to use this code:

Sub rename_Sheet()
Dim OldName As String
OldName = ActiveSheet.Name
On Error Resume Next
ActiveSheet.Name = Range("A1").Value
On Error GoTo 0
If OldName = ActiveSheet.Name Then
MsgBox "Worksheet not renamed, Illegal name or no data available"
End If
End Sub

but it does not work for me.

I am pretty sure I am doing something incorrectly.

I am wanting to rename my sheet tab based on Cell "DQ7" with is a date.

Any help would be greatly appreciated.
 

Attachments

  • 1708978035028.png
    1708978035028.png
    48.5 KB · Views: 13

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.
Try this line for the naming part:
VBA Code:
ActiveSheet.Name = Format(Range("DQ7"),"dd-mmm")
 
Upvote 0
try changing
VBA Code:
ActiveSheet.Name = Range("A1").Value
to
VBA Code:
ActiveSheet.Name = Range("DQ7").Text
 
Upvote 0
I am shill having trouble. getting this message.
1708982038821.png


I believe that i failed to mention that there are 7 associated sheets as follows:
sheet 1 to sheet 7

1708982236745.png
 
Upvote 0
It look like you already have a sheet named "25-Feb", from your image above.
 
Upvote 0
That is true, but I do not know how it got there a it does not go away.

I was thinking does this code need to be in each sheet or in the workbook?
 
Upvote 0
Why would it go away?

The point is this: you CANNOT have multiple sheets in the same workbook with the same name.
So if you already have a sheet named "25-Feb", you cannot name another sheet in that same workbook "25-Feb".
 
Upvote 0
Incorporating Joe's suggestion. Try this but be warned if the DQ7 sheet already exists it "will delete it"

VBA Code:
Sub rename_Sheet()
    Dim OldName As String
    Dim NewName As String
    
    OldName = ActiveSheet.Name
    NewName = Format(Range("DQ7"), "dd-mmm")
    
    If OldName = NewName Then
        MsgBox "Worksheet not renamed, Illegal name or no data available"
        Exit Sub
    End If
    
    On Error Resume Next
        Application.DisplayAlerts = False
            Worksheets(NewName).Delete
        Application.DisplayAlerts = True
        ActiveSheet.Name = NewName
    On Error GoTo 0
    
End Sub
 
Upvote 0
Why would it go away?

The point is this: you CANNOT have multiple sheets in the same workbook with the same name.
So if you already have a sheet named "25-Feb", you cannot name another sheet in that same workbook "25-Feb".
Joe4

Sorry, I an still verry new to all of this.

I was the "active Sheet" that was messing me up. I did not realize what sheet i was in when I ran the code.

I did get the original one to work (As long as I was in the correct sheet. LOL)

I am now see that I need to specify the sheet Numbers in the code: I have tried this:
Sub rename_Sheet1()
Dim OldName As String
Dim NewName As String

OldName = Worksheets(“Sheet1”).Name

NewName = Format(Range("F17"), "dd-mmm")

On Error GoTo 0
If OldName = ActiveSheet.Name Then
MsgBox "Worksheet not renamed, Illegal name or no data available"
End If
End Sub
----BUT I keep getting an error message
1709051044365.png


From above:
In “Sheet1” I am wanting the contents from "F17" to be returned as the "NewName" for the worksheet.

Also, How is this updated? I suspect that every time cell "F17" is changed/update the Worksheet Name would automatically be up dated as well, Is this correct?

With that being said there are 7 sheets in total: (I am trying to get all sheets to have the worksheet names to auto populate for the current week that i am in (SUN ---> SAT) (2-25 --->3-1)

Sheet1” = Cell "F17"
Sheet2” = Cell "G17"
Sheet3” = Cell "H17"
Sheet4” = Cell "I17"
Sheet5” = Cell "J17"
Sheet6” = Cell "K17"
Sheet7” = Cell "L17"

Thank you in advance.
 
Upvote 0
Replace all the slanted double-quotes with straight ones.
VBA does NOT like slanted double-quotes!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,181
Members
452,615
Latest member
bogeys2birdies

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