If you always add and name a new Worksheet with specific name in your Workbook, sometimes it seems very boring to do it manually. This simple macro can help you.
Add and Name a New Worksheet with Current Date
Sub AddAndNameWithCurrentDate() 'Step 1: Tell Excel what to do if Error On Error GoTo Error 'Step 2: Add a new Worksheet and name it with specific name Sheets.Add 'add a new worksheet, by default, the sheet is called Sheetxx ActiveSheet.Name = Format(Date, "dd-mm-yyyy") 'name with current date, if you just want to add a sheet, delete this row. Exit Sub 'Step 3: If here, an error happened; tell the user Error: MsgBox "There is already a Worksheet called that." End Sub
Add and Name a New Worksheet with Specific Name
Sub AddAndNameWithSpecificName() 'Step 1: Tell Excel what to do if Error On Error GoTo Error 'Step 2: Add a new Worksheet and name it with specific name Sheets.Add.Name = "My New Sheet" 'add a new Worksheet and name with "My New Sheet", change the name with yours. Exit Sub 'Step 3: If here, an error happened; tell the user Error: MsgBox "There is already a Worksheet called that." End Sub
How This Macro Works
- If you give the new sheet a name that already exists, an error occurs. In Step 1, the macro tells Excel to immediately skip to the line that says Error (in Step 3) if there is an error.
- In Step 2, we use the Add method to add a new Worksheet. By default, the Worksheet is named Sheetxx by Excel, where xx represents the number of the Worksheet counts, eg. Sheet7, Sheet15.
- In macro AddAndNameWithCurrentDate, We give the sheet a new name by changing the Name property of the ActiveSheet object. In this case, we use Format function name the worksheet with the current date, you can change the format as per your requirement.
- In macro AddAndNameWithSpecificName, We use the Add method to add a new Worksheet and name with specific name "My New Sheet", you can change the name with yours.
- Step 3 notifies the user that the Worksheet name already exists.
How to Use This Macro
To use this macro, you can copy and paste it into a standard module:
- Activate the Visual Basic Editor by pressing
- Right-click the project/workbook name in the Project window.
- Type or paste the code in the newly created module.