Tab Name based on Cell Value

excelnoobhere

Board Regular
Joined
Mar 11, 2019
Messages
50
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,206
Office Version
365
Platform
Windows
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
 

excelnoobhere

Board Regular
Joined
Mar 11, 2019
Messages
50
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 :)
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,815
Office Version
2007
Platform
Windows
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
 

excelnoobhere

Board Regular
Joined
Mar 11, 2019
Messages
50
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,815
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,081,971
Messages
5,362,492
Members
400,677
Latest member
champchamp

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top