Sheet naming with VBA based on cell value

Javi

Active Member
Joined
May 26, 2011
Messages
438
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
 

Some videos you may like

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.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
48,138
Office Version
  1. 365
Platform
  1. Windows
What is the sheet codename for sheet1?
 

Javi

Active Member
Joined
May 26, 2011
Messages
438
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
48,138
Office Version
  1. 365
Platform
  1. Windows
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.
 

Javi

Active Member
Joined
May 26, 2011
Messages
438

ADVERTISEMENT

Please see below... I will be adding many more sheets at some point.

1599925239986.png
 

Javi

Active Member
Joined
May 26, 2011
Messages
438
Sheet8 will also need to remain static as this is my print selection sheet.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
48,138
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 

Javi

Active Member
Joined
May 26, 2011
Messages
438
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.
 

Javi

Active Member
Joined
May 26, 2011
Messages
438
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
 

Watch MrExcel Video

Forum statistics

Threads
1,114,035
Messages
5,545,640
Members
410,696
Latest member
JTrehan
Top