Tab Name based on Cell Value

excelnoobhere

Board Regular
Joined
Mar 11, 2019
Messages
61
I have the following code that looks at AC1 and renames the tabs in the entire workbook based on the cell value and if there is no name or any unacceptable characters it gives an error message. I want this to happen only for the active sheet and not to loop through each sheet. I know it is just a one line change but I'm not sure how. I would still like to keep all the error checking and messages.
thanx in advance

Code:
Sub tabname()Dim ws As Worksheet
For Each ws In Worksheets
    On Error Resume Next
    If Len(ws.Range("AC1")) > 0 Then
        ws.Name = ws.Range("AC1").Value
    End If
    On Error GoTo 0
    If ws.Name <> ws.Range("AC1").Value Then
        MsgBox ws.Name & " The sheet was Not renamed, the suggested name was invalid"
    End If
Next
End Sub
 

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.
How about
Code:
Sub tabname()
   Dim ws As Worksheet
   Set ws = ActiveSheet
    On Error Resume Next
    If Len(ws.Range("AC1")) > 0 Then
        ws.Name = ws.Range("AC1").Value
    End If
    On Error GoTo 0
    If ws.Name <> ws.Range("AC1").Value Then
        MsgBox ws.Name & " The sheet was Not renamed, the suggested name was invalid"
    End If
End Sub
 
Upvote 0
How about
Code:
Sub tabname()
   Dim ws As Worksheet
   Set ws = ActiveSheet
    On Error Resume Next
    If Len(ws.Range("AC1")) > 0 Then
        ws.Name = ws.Range("AC1").Value
    End If
    On Error GoTo 0
    If ws.Name <> ws.Range("AC1").Value Then
        MsgBox ws.Name & " The sheet was Not renamed, the suggested name was invalid"
    End If
End Sub

that worked perfectly, Thank you :)
 
Upvote 0
Try this


Code:
Sub tabname()
    Dim ws As Worksheet, NewName As String, exists As Boolean
    
    NewName = ActiveSheet.Range("AC1").Value
    If NewName = "" Then
        MsgBox "There is no sheet name in AC1"
        Exit Sub
    End If
    '
    'Verify if a sheet with the new name already exists
    exists = False
    For Each ws In Worksheets
        If LCase(ws.Name) = LCase(NewName) Then
            exists = True
            Exit For
        End If
    Next
    '
    If exists = False Then
        ActiveSheet.Name = NewName
    Else
        MsgBox ActiveSheet.Name & " The sheet was Not renamed, the suggested name was invalid"
    End If
End Sub
 
Upvote 0
Try this


Code:
Sub tabname()
    Dim ws As Worksheet, NewName As String, exists As Boolean
    
    NewName = ActiveSheet.Range("AC1").Value
    If NewName = "" Then
        MsgBox "There is no sheet name in AC1"
        Exit Sub
    End If
    '
    'Verify if a sheet with the new name already exists
    exists = False
    For Each ws In Worksheets
        If LCase(ws.Name) = LCase(NewName) Then
            exists = True
            Exit For
        End If
    Next
    '
    If exists = False Then
        ActiveSheet.Name = NewName
    Else
        MsgBox ActiveSheet.Name & " The sheet was Not renamed, the suggested name was invalid"
    End If
End Sub
this also worked thank you
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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