How do I get the tab name equal to the contents of a cell? For example, if cell content A1
is 01
, the tab name is 01
too.
How do I get the tab name equal to the contents of a cell? For example, if cell content A1
is 01
, the tab name is 01
too.
You will need to use VBA to do what you want.
My first attempt was to create a function that you could use directly in the formula. But that did not work (it just did not update the Name
property of the tab, probably because Excel was calculating formulas at that time).
The second attempt worked, based on the event of cell selection change in the worksheet. Just open the VBA editor and enter the following code:
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 "Por favor, revise o nome na célula A1." & Chr(13) _
& "Parece que ele contém um ou mais caracteres ilegais." & Chr(13)
Range("A1").Activate
End Sub
Note that this code assumes that the user is typing the value of that cell, and therefore displays an error message in case of problems updating the name of the tab. If your cell has automatic updating (from another source other than the user), you will probably want to remove this message display and replace it with an automated treatment (perhaps setting a default title, for example).
Do not forget to save your macros-enabled spreadsheet (* .xlsm extension).
PS: I was about to make a code when I found it ready (I just translated the text). The source is that site .