Sheet naming with VBA based on cell value

Javi

Active Member
Joined
May 26, 2011
Messages
440
Hi All,


I’m trying to modify one of the two codes below so that the work sheet needing the name change does not require that specific sheet to be active or require manual intervention.
This workbook has two sheets currently many more to be added later.

  • “Main” summary sheet with data entry fields
  • “Sheet1” all data on this worksheet will be referenced from “Main” this worksheet will not be active unless the user is going to print. No edits will ever be made directly on this sheet.


On worksheet “Main” cell C5 contains the name of what I would like “Sheet1” to be renamed

On worksheet “Sheet1” cell A1 is referencing “Main C5”

This configuration works fine with the two below codes, provided I activate sheet1 one and highlight cell A1 and hit enter or make a change somewhere on the sheet.

What I’m trying to accomplish is "Sheet1" to change names without needing any other intervention. When the cell value on worksheet “Main” C5 is changed or modified I would like the worksheet on "Sheet1" name to just change.

Sorry for being so wordy on what maybe should be a very simple question?

My preference would be to use code 2 below as I prefer the error handling but if that’s not possible no big deal.

Code 1

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set Target = Range("A1")
If Target = "" Then Exit Sub
Application.ActiveSheet.Name = VBA.Left(Target, 31)
Exit Sub
End Sub





Code 2

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    'Specify the target cell whose entry shall be the sheet tab name.
    If Target.Address <> "$A$1" Then Exit Sub
        'If the target cell is empty (contents cleared) then do not change the sheet name
    If IsEmpty(Target) Then Exit Sub

    'If the length of the target cell's entry is greater than 31 characters, disallow the entry.
    If Len(Target.Value) > 31 Then
        MsgBox "Worksheet tab names cannot be greater than 31 characters in length." & vbCrLf & _
        "You entered " & Target.Value & ", which has " & Len(Target.Value) & " characters.", , "Keep it under 31 characters"
        Application.EnableEvents = False
        Target.ClearContents
        Application.EnableEvents = True
        Exit Sub
    End If

    'Sheet tab names cannot contain the characters /, \, [, ], *, ?, or :.
    'Verify that none of these characters are present in the cell's entry.
    Dim IllegalCharacter(1 To 7) As String, i As Integer
    IllegalCharacter(1) = "/"
    IllegalCharacter(2) = "\"
    IllegalCharacter(3) = "["
    IllegalCharacter(4) = "]"
    IllegalCharacter(5) = "*"
    IllegalCharacter(6) = "?"
    IllegalCharacter(7) = ":"
    For i = 1 To 7
        If InStr(Target.Value, (IllegalCharacter(i))) > 0 Then
            MsgBox "You used a character that violates sheet naming rules." & vbCrLf & vbCrLf & _
            "Please re-enter a sheet name without the ''" & IllegalCharacter(i) & "'' character.", 48, "Not a possible sheet name !!"
            Application.EnableEvents = False
            Target.ClearContents
            Application.EnableEvents = True
            Exit Sub
        End If
    Next i

    'Verify that the proposed sheet name does not already exist in the workbook.
    Dim strSheetName As String, wks As Worksheet, bln As Boolean
    strSheetName = Trim(Target.Value)
    On Error Resume Next
    Set wks = ActiveWorkbook.Worksheets(strSheetName)
    On Error Resume Next
    If Not wks Is Nothing Then
        bln = True
    Else
        bln = False
        Err.Clear
    End If

    'If the worksheet name does not already exist, name the active sheet as the target cell value.
    'Otherwise, advise the user that duplicate sheet names are not allowed.
    If bln = False Then
        ActiveSheet.Name = strSheetName
    Else
        MsgBox "There is already a sheet named " & strSheetName & "." & vbCrLf & _
        "Please enter a unique name for this sheet."
        Application.EnableEvents = False
        Target.ClearContents
        Application.EnableEvents = True
    End If

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
What is the sheet codename for sheet1?
 
Upvote 0
Thank you for the reply

Sheet1 is my main summary sheet and I have it named Main. I'm not sure if that's what you're asking. Sheet one will never need to be renamed it is the data entry sheet.
 
Upvote 0
In the VB Editor you will see something like this
Sheet codenames.png

Where the part in brackets is the actual name of the sheet (as seen on the tab) & the part before the brackets is the codename.
I need the codename for the sheet you want to rename.
 
Upvote 0
Put this in the sheet module for Main
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "C5" Then
      If IsSheetNameOk(Target.Value) Then
         Sheet2.Name = Target.Value
      Else
         MsgBox "sheetname " & Target & " is not valid, or already taken"
      End If
   End If
End Sub
And it will rename the sheet called hope, whenever you change the value in C5 on Main

Also put this in a standard module
VBA Code:
Function IsSheetNameOk(ShtName As String) As Boolean
   IsSheetNameOk = False
   If Len(ShtName) < 32 And Not ShtName = "" And Not ShtName Like "*[:/\*[?]*" And Not ShtName Like "*[]]*" And LCase(ShtName) <> "history" Then
      If Not Evaluate("isref('" & ShtName & "'!A1)") Then
         IsSheetNameOk = True
      End If
   End If
End Function
 
Upvote 0
I must be doing something wrong. No errors and also no changes to the sheet names.

I right clicked on the worksheet "Hope" view code and pasted the first code.

Then I created a new module and entered the second code.

Made changes to cell C5 on Sheet1 "Main" no go.
 
Upvote 0
Sorry I apologize for the confusion it totally worked.

Could you please help me with the your below code.

I will need to add additional sheets how would I handle that.



VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "C5" Then
      If IsSheetNameOk(Target.Value) Then
         Sheet2.Name = Target.Value
      Else
         MsgBox "sheetname " & Target & " is not valid, or already taken"
      End If
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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