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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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