Written by Allen Wyatt (last updated November 14, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
Excel lets you create new worksheets in a number of different ways. What if you want to create a new worksheet and name it all in one step? The easiest way to do this is with a macro. The following is an example of a macro that will ask for a name, and then create a worksheet and give that worksheet the name provided.
Sub AddNameNewSheet1() Dim Newname As String Newname = InputBox("Name for new worksheet?") If Newname <> "" Then Sheets.Add Type:=xlWorksheet ActiveSheet.Name = Newname End If End Sub
This macro works fine, as long as the user enters a worksheet name that is "legal" by Excel standards. If the new name is not acceptable to Excel, the worksheet is still added, but it is not renamed as expected.
A more robust macro would anticipate possible errors in naming a worksheet. The following example code will add the worksheet, but keep asking for a worksheet name if an incorrect one is supplied.
Sub AddNameNewSheet2() Dim CurrentSheetName As String 'Remember where we started CurrentSheetName = ActiveSheet.Name 'Add New Sheet Sheets.Add 'Make sure the name is valid On Error Resume Next 'Get the new name ActiveSheet.Name = InputBox("Name for new worksheet?") 'Keep asking for name if name is invalid Do Until Err.Number = 0 Err.Clear ActiveSheet.Name = InputBox("Try Again!" _ & vbCrLf & "Invalid Name or Name Already Exists" _ & vbCrLf & "Please name the New Sheet") Loop On Error GoTo 0 'Go back to where you started Sheets(CurrentSheetName).Select End Sub
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10846) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Excel here: Creating Worksheets with a Macro.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Ever want to have Excel run a procedure whenever you open a workbook? It's not as difficult as you might think. Here's how.
Discover MoreHaving problems with using macros in a protected workbook? There could be any number of causes (and solutions) as ...
Discover MoreWhen writing macros, you may want to position a message box at a specific location on the screen. This can't be done in ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-04-10 06:05:59
Chris
In order to give the user guidance about sheet naming I used the following prompt string for the input box:
Prompt_String = "This will add a new sheet to the left of " & CurrentSheetName _
& vbCrLf _
& vbCrLf & "Enter a name for the new sheet, but note that" _
& vbCrLf & "* these characters are invalid: \ / * [ ] : ? " _
& vbCrLf & "* names longer than 31 characters are invalid," _
& vbCrLf & "* the names of existing sheets cannot be used, and" _
& vbCrLf & "* the name 'History' is invalid."
where I used the rules shown in https://www.accountingweb.com/technology/excel/seven-characters-you-cant-use-in-worksheet-names .
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments