Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Creating Worksheets with a Macro.

Creating Worksheets with a Macro

Written by Allen Wyatt (last updated April 10, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10846) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Creating Worksheets with a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Links to Hyperlinks

Hyperlinks in a worksheet can be helpful or essential, depending on the nature of your data. If you create a link to a ...

Discover More

Pasting Numeric Values in Other Programs

When you paste information from Excel into other programs, you may get more than you actually want. It is not unusual for ...

Discover More

Writing an Excel Help File

If you develop applications or add-ins using VBA, you may want to create a help file that supports your project. This tip ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Pulling Cell Names into VBA

Excel allows you to define names that can refer either to ranges of cells or to constant information, such as formulas. ...

Discover More

Selecting a Cell in the Current Row

Macros often need to select different cells in a worksheet. Here's how you can use macro commands to change which cell is ...

Discover More

Determining a Random Value

Random values are often needed when working with certain types of data. When you need to generate a random value in a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four minus 0?

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 .


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.