Macro to name worksheet based on cell A1

AntBlabby8

Board Regular
Joined
Apr 18, 2002
Messages
197
Suppose I want to each worksheet to take it's name from what I key into Cell A1 on that worksheet. So if I want the first tab to be called and show "California," I would key "California" into Cell A1 on that worksheet. And if I wanted the second to be called "New York," I'd key that into A1 on that worksheet. And then, if later on, I change my mind and want to call the tabs "CA", "NY", etc., I'd just have to change what's in A1. Is there a MACRO that I can put in my workbook to do this, as I have many, many worksheet templates and these would be TERRIFIC if I could do it!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this, but it would need to go in each sheet:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Activesheet.Name = Range("A1").Value
End Sub

Or put this in a module and run the macro whenever you want all the sheets to be renamed:

Code:
Sub ChangeSheetNames()
dim ws as Worksheet
For each ws in Worksheets
ws.Name = ws.Range("A1").Value
Next ws
End Sub
EDIT: You may want to put in some edit checks for special characters that sheet names can't have to prevent the macro from crashing.
 
Last edited:
Upvote 0
You can make it work for every sheet using the Workbook Sheet Change event:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
Sh.Name = Sh.Range("A1")
End Sub
 
Upvote 0
As previously mentioned, the code provided above doesn't check for valid sheet names. So if the value in cell A1 is an invalid sheet name the code will crash.

I believe the following code checks for all requirements regarding sheet names, and will work for any sheet in the workbook (put it in the ThisWorkbook module, of course):

1. Max of 31 characters
2. No duplicate sheet names in workbook (both before and after replacing special characters)
3. No special characters, which includes / \ [ ] : ? *

Any special characters are replaced with an underscore character. To change this, change the character assigned to the skipChr variable. If you want to make it blank, you'll have to add a check in the code to see if the newStr value is empty at the end (before you assign it to ActiveSheet.Name) otherwise the code would crash if you set A1 to all special characters. I hope that makes sense.
Rich (BB code):
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim i As Long, myChr As String, skipChr As String
    Dim newStr As String, oldStr As String, ws As Worksheet
    
    skipChr = "_"
    oldStr = Range("A1").Value
    newStr = ""
 
    If Not Intersect(Target, Range("A1")) Is Nothing Then
        ' Check to see if length of A1 is <= 31
        If Len(oldStr) > 31 Then
            oldStr = Left(oldStr, 31)
        End If
 
      ' Check to see if sheet name already exists as typed
        On Error Resume Next
        Set ws = Worksheets(oldStr)
        On Error GoTo 0
        If Not ws Is Nothing Then
            MsgBox "A sheet with the name """ & oldStr & """ already exists."
            Exit Sub
        End If
 
      ' Check to see if A1 is now blank (e.g. deleted cell A1)
        If Range("A1").Value = "" Then
            Exit Sub
        Else
          ' Check each character for special characters
          ' that cannot be used in Sheet names
            For i = 1 To Len(oldStr)
                myChr = Mid(oldStr, i, 1)
                Select Case myChr
                    Case Is = ":", "/", "\", "?", "[", "]", Chr(42)
                        oldStr = Replace(Expression:=oldStr, _
                            Find:=myChr, Replace:=skipChr)
                        newStr = oldStr
                    Case Else
                        newStr = oldStr
                End Select
            Next i
        End If
 
        ' Check to see if new (altered) sheet name exists
        On Error Resume Next
        Set ws = Worksheets(newStr)
        On Error GoTo 0
        If Not ws Is Nothing Then
            MsgBox "A sheet with the name """ & newStr & """ already exists."
            Exit Sub
        End If
 
    ActiveSheet.Name = newStr
    End If
End Sub
 
Last edited:
Upvote 0
This is a different approach to error handling. (In ThisWorkbook code module)
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)
Dim newName As String
If Target.Address = "$A$1" Then
    newName = CStr(Target.Value)
    If newName <> vbNullString Then
        On Error Resume Next
            Sh.Name = newName
            If Err <> 0 Then
                If Sh.Parent.Sheets(newName).Index < 1 Then
                    MsgBox "Illegal Sheet name."
                Else
                    MsgBox "That name already exists."
                End If
                Application.Undo
            End If
        On Error GoTo 0
    End If
End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,214
Members
449,091
Latest member
jeremy_bp001

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