Make tab name equal to the contents of a cell in Excel

5

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.

    
asked by anonymous 15.10.2015 / 00:39

1 answer

6

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 .

    
15.10.2015 / 04:15