Change tab name based on cell value containing formula

yomarcos

New Member
Joined
Aug 13, 2021
Messages
39
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I have recently started to use VBA so my knowledge is VERY basic. I found online a way to change a tab name based on cell value but it doesn't work straight away because the cell contains a formula (which is just =Sheet1!A3)

I have been trying to add Sub Worksheet_Calculate() to the code I already have but I keep failing.

Could anyone help me please?

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Set Target = Range("A1")
If Target = "" Then Exit Sub
On Error GoTo Badname
ActiveSheet.Name = Left(Target, 31)
Exit Sub
Badname:
MsgBox "Please revise the entry for this feature." & Chr(13) _
& "It appears to contain one or more " & Chr(13) _
& "illegal characters." & Chr(13)
Range("A1").Activate
End Sub

Thank you!!
 
If people may be messing with the order of the sheets so that my assuption won't work for you, then you could use this code, but you would need to put it in EVERY sheet module (other than Project):
VBA Code:
Private Sub Worksheet_Calculate()
   
    On Error GoTo err_chk
'   Update sheet name if it changed
    If Me.Name <> Left(Me.Range("A1"),31) Then Me.Name = Left(Me.Range("A1"),31)
    On Error GoTo 0
   
    Exit Sub
   
err_chk:
'   Error handling code
    MsgBox Err.Number & ":" & Err.Description
   
End Sub
Obviously, the other way would be preferrable, as you would only have to put the code in one place as opposed to 50.
 
Upvote 0
Solution

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
If people may be messing with the order of the sheets so that my assuption won't work for you, then you could use this code, but you would need to put it in EVERY sheet module (other than Project):
VBA Code:
Private Sub Worksheet_Calculate()
  
    On Error GoTo err_chk
'   Update sheet name if it changed
    If Me.Name <> Left(Me.Range("A1"),31) Then Me.Name = Left(Me.Range("A1"),31)
    On Error GoTo 0
  
    Exit Sub
  
err_chk:
'   Error handling code
    MsgBox Err.Number & ":" & Err.Description
  
End Sub
Obviously, the other way would be preferrable, as you would only have to put the code in one place as opposed to 50.
This is exactly what I wanted. Thank you very much!!!
 
Upvote 0
Hi Joe4,

I have one more question for you. Is it possible to add something to this code that you sent me that would make impossible for users to change the sheets names manually?
 
Upvote 0
You can use one of the methods mentioned here: How to prevent from changing sheet name in Excel?

Note that if you use the Worksheet protection option, what you would do is add code to all the "Worksheet_Calculate" sub procedures that first unprotects the sheet, makes the name change, and then re-protects the sheet at the end. That is how you make it so that the code can change the sheet names, but no one can manually.
 
Upvote 0
You can use one of the methods mentioned here: How to prevent from changing sheet name in Excel?

Note that if you use the Worksheet protection option, what you would do is add code to all the "Worksheet_Calculate" sub procedures that first unprotects the sheet, makes the name change, and then re-protects the sheet at the end. That is how you make it so that the code can change the sheet names, but no one can manually.
Thank you!!! Problem solved
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

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