Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Creating Worksheets with a Macro.
by Allen Wyatt
(last updated September 23, 2017)
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 'Not needed if you don't want to return 'to where you started but want to stay 'on the New Sheet 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 'Not needed if you don't want to return 'to where you started but want to stay 'on the New Sheet Sheets(CurrentSheetName).Select End Sub
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2022) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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!
Part of writing macros is to make sure they work as you expect. This involves a process known as debugging. Here's how ...Discover More
Click a button on a toolbar and Excel will go so far as to open a another workbook in order to run a macro associated ...Discover More
When processing information using a macro, you may need to know if there are any other instances of Excel running on a ...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 menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.