Write an Excel Macro in VBA to Update Worksheet Tab Name

Characters Not Allowed in Worksheet Names

Characters Not Allowed in Worksheet Names

This is Part 2 of my two-part series of video tutorials where I demonstrate how to link an Excel Worksheet Tab Name and the contents of a Worksheet Cell.

In this lesson, I show you how to write an Excel Macro in VBA (Visual Basic for Applications) that will update the name of the Worksheet Tab based on the value of a cell in that worksheet.

Write Excel Macro in VBA

For this Macro, the first line of code will prevent the Macro from crashing if the cell contains one of the “disallowed” characters that you see in the diagram

On Error Resume Next

The second line of code initiates a “For – Next” loop of instruction

For Each ws In Thisworkbook.Worksheets

The third line of code is the Instruction to follow in the “For – Next” Loop

ws.Name = Left(ws.Cells(1,5).Value, 31)

Here, this instruction is saying – in plain English – “Nane this Worksheet (ws) using the leftmost 31 characters in the cell that is in the 1st row of the 5th column – (Cell E1)”

The reason for including 31, is that that is the maximum number of characters allowed in an Excel Worksheet Tab Name.

The fourth and fifth lines of code complete the macro – watch the video to see what they are (What a “tease” I am!!)

Run Macro from Command Button

In this lesson I also show you how to run this Macro from a command button that I add to the Quick Access Toolbar in Excel 2007 or Excel 2010.

Watch Video in High Definition on YouTube

 Click on this link to watch this Excel Video Tutorial in High Definition on my YouTube Channel – DannyRocksExcels

Source for This Code

I found the code for this example in one of Bill Jelen, Mr. Excel’s Books, “Excel Gurus Gone Wild.”

Bill’s website is: http://www.mrexcel.com/

Watch Part 1 of this Series

Here is the link to Part 1 of this series. I show you how to write a Custom Excel Function to insert the Name of the Worksheet Tab into a cell on the worksheet.

Dynamically Update a Cell with an Excel Worksheet Name

Formula to Capture Worksheet Name

Capture Tab Name in Cell

I am excited to share this Excel Tip with you! Several viewers have recently asked if there was a way to create a link in a cell to the name of a worksheet tab. Yes there is! And, in this lesson, I demonstrate how to do this.

Say, for example, that you want Cell C1 to contain the name of the current worksheet (Sheet1). With this formula, when you change the name of the worksheet to “January,” cell C1 is dynamically updated to show “January.”

Perfect for Excel Templates

If you use a template for your monthly, regional sales or financial reports, then this Excel formula is essential. You write the formula once – using Group edit – and each worksheet in your Excel workbook will be linked to a cell in your report. Change the name of the worksheet and your report name is automatically updated.

Functions Used in this Formula

  • CELL() – to gather information about the worksheet – in this case, the “Filename.”
  • MID() – to extract the characters in a text string – e,g, the “worksheet name.”
  • FIND() – to locate the starting point for the MID() function to extract the characters in the text.

As the final argument for the MID() Function, I use 31 characters because that is the maximum number of characters that you can use when naming an Excel worksheet.

Try this for yourself. Let me know how it works for you. Add your comments below.

Download Excel Workbook for this Lesson

Watch Video in High Definition

Follow this link to view this video tutorial on my YouTube Channel – DannyRocksExcels

I invite you to watch or to subscribe to my video podcast on iTunes. Follow this link to learn more about my RSS Feed.

Rename and group worksheets in Excel 2003

Here are the “tips and time-savers” in today’s lesson:

  1. Rename your worksheets – give them a more descriptive name
  2. Insert a new worksheet in your workbook
  3. Use “Tab Color” to better organize your worksheets
  4. Group your worksheets to improve efficiency & accuracy of data entry & formatting
  5. Use “AutoFill” to speed up data entry

Find the video lesson that you want – Index to all Excel Topics

New! My DVD, “The 50 Best Tips for Excel 2007,” is now available for purchase. I invite you to visit my online bookstore for more information.

Click here to see a listing of “The 50 Best Tips for Excel 2007.”