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,084,738
Messages
5,379,524
Members
401,611
Latest member
CandaceR68

Some videos you may like

This Week's Hot Topics

  • VBA code giving errors and stopping Excel
    Hello Experts, I have this code being used to loop through files in a file path, and copy specific data to another sheet. It is giving me several...
  • Disable MsgBox message
    Morning, I have a userform where if i leave a ComboBox empty i see a MsgBox warning me that i must enter an invoice number. It is this MsgBox i...
  • Macro Recorder into VBA, Copy Paste Data Filled Cells
    Hi Everyone, I have a macro recorder file that takes a selection of data, copies, then pastes into a new sheet on ("A2:B2") The issue is my...
  • Number format changes while pasting into a cell
    Hi, I am trying to paste a number 180204524303 from an email to an excel cell, however, whenever i try to do so , the the paste value appears as...
  • Collating data
    Hello all. Could someone please help. I am trying to pull all column data from multiple sheets (24 I total so far) into 1 master sheet without...
  • Sum Multiple Columns Based on Multiple Criteria
    I am trying to consolidate data by summing columns G through M based on material, plant, vendor, and fiscal year being identical. The period does...
Top