Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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.
by Allen Wyatt
(last updated December 6, 2014)
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
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10846) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Creating Worksheets with a Macro.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
When processing information in a macro, you often need to select different cells relative to the currently selected ...Discover More
Do you get tired of the dialog box that says "do you want to enable macros" that is displayed when you open a workbook. ...Discover More
When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.