Problem with tab names equaling cell name.

cole516gray

New Member
Joined
Dec 2, 2013
Messages
6
I found a code to make the tab name equal a cell in the worksheet:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 Const WS_RANGE As String = "ad1" '<== change to suit
 On Error GoTo ws_exit
 Application.EnableEvents = False
 If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
 With Target
 Me.Name = .Value
 End With
 End If
ws_exit:
 Application.EnableEvents = True
 End Sub
This worked great for the first worksheet, but it is not working for any of the other sheets. Can anyone tell me if I am missing something in the code?
 
Last edited by a moderator:

nigelk

Well-known Member
Joined
Aug 30, 2008
Messages
535
You're turning events off but only turning them on again if you encounter an error.

Try something along these lines:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Const WS_RANGE As String = "ad1" '<== change to suit
On Error GoTo ws_exit


Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
Me.Name = .Value
End With
End If

Application.EnableEvents = True

Exit Sub



ws_exit:
Application.EnableEvents = True
MsgBox "Sheet NOT named."

End Sub
 

cole516gray

New Member
Joined
Dec 2, 2013
Messages
6
I am now copying the tabs and the tabs are initially being renamed, however if I change the cell name after the initial time of renaming the cell, the tab name is not adjusting accordingly. Is there a way to make it always adjust with the cell name or are there specific conditions it will work in?
 

nigelk

Well-known Member
Joined
Aug 30, 2008
Messages
535
Slightly different., it doesn't rely on the name of the cell.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim Tcell As Range

Set Tcell = ActiveSheet.Range("AD1")

If Target <> Tcell Then Exit Sub

On Error GoTo ws_exit

Application.EnableEvents = False
ActiveSheet.Name = Tcell
Application.EnableEvents = True

Exit Sub

ws_exit:
Application.EnableEvents = True
MsgBox "Sheet NOT named."

End Sub
 

nigelk

Well-known Member
Joined
Aug 30, 2008
Messages
535
This will only work with the sheet that the code is in by the way. It won't have any effect on other sheets that do not contain the above code.
 

Forum statistics

Threads
1,081,765
Messages
5,361,156
Members
400,615
Latest member
inzimam

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