Excel.Tips.Net Welcome toExcel.Tips.Net

Helpful Links

Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment

Tips.Net Store

ExcelTips FAQ
ExcelTips Premium

Learn Access Now
Free Printable Forms

Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips

Advertise on the
ExcelTips Site

Newest Tips

Recording a Macro

Adding a Little Animation to Your Life

Converting a Range of URLs to Hyperlinks

Making the Formula Bar Persistent

Engineering Calculations

Digital Signatures for Macros

Fixing the Decimal Point

 

Dynamic Worksheet Tab Names

Summary: Would it be helpful if your worksheet tab names were determined by the contents of a cell in your worksheet? Using the macros described in this tip, you can easily make the worksheet tabs just as dynamic as any other part of an Excel worksheet. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)

You probably already know that you can change the name of a worksheet tab by double-clicking on the tab and providing a new name. What if you want to do it dynamically, however? What if you want to have the value in cell A1 automatically appear as the tab name?

Unfortunately, Excel doesn't provide an intrinsic function to handle this sort of task. It is a relatively simply task to develop one using a macro that will do the job for you. For instance, the following macro will change the tab name to the contents of A1:

Sub myTabName()
    ActiveSheet.Name = ActiveSheet.Range("A1")
End Sub

There are several important items to note about this macro. First of all, there is no error checking. This means that if A1 contains a value that would be illegal for a tab name (such as nothing at all or more than 31 characters), then the macro generates an error. Second, the macro must be manually run.

What if you want a more robust macro that does check for errors and does run automatically? The result is a bit longer, but still not overly complex:

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 in A1." & Chr(13) _
    & "It appears to contain one or more " & Chr(13) _
    & "illegal characters." & Chr(13)
    Range("A1").Activate
End Sub

To set up this macro, follow these steps:

  1. Open a new workbook that has only one worksheet in it.
  2. Press Alt+F11 to open the VBA Editor.
  3. In the Project Explorer (upper-left corner of the VBA Editor), locate and double-click on the Excel object entitled Sheet1, within the Book1 project. (You should note that the project name may be different than Book1.) VBA opens a code window for Sheet1.
  4. Paste (or type) the above macro into the code window.
  5. Close the VBA Editor.
  6. Locate the XLStart folder on your system. (Use the Find option from the Start menu.)
  7. Save the workbook as an Excel template using the name BOOK.XLT in the XLStart directory. This causes the template to become your pattern for any new workbook you create.
  8. Again save the workbook as a template in the same directory, this time using the name SHEET.XLT. This causes the template to become the pattern for any new worksheets you insert in a workbook.
  9. Close and restart Excel.

Now, anytime you change the value in cell A1, the worksheet tab also updates.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2145) applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003

More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.
 
Check out ExcelTips: The Macros today!