When you add a new sheet in a workbook, you have the option to name it. But you can also rename it any time using the name property of the worksheet. In this tutorial, we will look at different ways to rename a sheet or multiple sheets using a VBA code. Show
Steps to Rename a Sheet using a VBA Code
Get ActiveSheet NameThis will display the ActiveSheet name in a message box:
Get Sheet Name by index NumberThis will display the first worksheet name in a message box:
This will display the name of the last worksheet in the workbook:
Get Sheet Name by Code NameIn the VBA Editor, there is an option to change the “code name” of a Sheet. The code name is not visible to the Excel user and can only be seen in the VBA Editor: In VBA, when working with Sheets, you can reference the usual Tab name:
or the VBA code name:
Referencing the code name is desirable in case the Sheet tab name ever changes. If you allow you Excel user access to changing sheet names you should reference the code name in your VBA code so that a Sheet tab name mismatch doesn’t cause an error. Sheet code names are discussed in more detail here. To get the Sheet name using the VBA Code name, do the following:
Rename SheetYou can rename Sheets by adjusting the name property of the Sheets or Worksheets object. Rename ActiveSheet
VBA Coding Made EasyStop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! Learn More Rename Sheet by Name
Rename Sheet by Sheet Index NumberHere we use 1 to rename the first Sheet in the Workbook.
Rename Sheet by Code NameThis code will rename a sheet using it’s VBA code name (discussed above):
VBA Programming | Code Generator does work for you! Check if Sheet Name ExistsWe created a function to test if a Sheet with a particular name already exists. 0The function will return TRUE if the Sheet exists, or FALSE if it does not. Use the function like so: 1
Copy Sheet and RenameThis example is from our article on Copying Sheets. After copying and pasting a Sheet, the newly created sheet becomes the ActiveSheet. So to rename a copied Sheet, simply use ActiveSheet.Name: |