Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Creating Worksheets with a Macro

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.

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.

Related Tips:

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!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Alan Elston    16 Dec 2016, 05:59
Hi QLO,
I always forget the syntax with these things. They are the sort of things that are very easy to get by doing a macro recording whilst doing it manually. The code you then get is very easy to understand and modify. You end up with something like this:
Sub NewSheet()
Dim wsOld As Worksheet, wsNew As Worksheet
 Set wsOld = ThisWorkbook.Worksheets("OldSheet") ' - Change to suit your current Worksheet Name
 wsOld.Copy After:=ThisWorkbook.Worksheets.Item(ThisWorkbook.Worksheets.Count) ' Copy Place after last Worksheet Item Number (Count of Worksheets is equal to last tab counting from left which is the last Worksheet Item Number ))
 Set wsNew = ActiveSheet
 Let wsNew.Name = "NewSheet" ' - Change to suit the new name you want
End Sub
Barry    16 Dec 2016, 05:34
@QLO

Just substitute:
Worksheets("SourceSheetName").Copy instead of Sheets.Add

 Change "SourceSheetName" to the name of the original sheet that you want to be copied.
QLO    16 Dec 2016, 03:32
Hi,

I know the article is old.
But i have a question.

If i use the code above, but want it to copy/paste a sheet every time, so it will be the same data in the new sheet but with an new sheet name, is this possible to include in the macro?
Valentin    01 Aug 2016, 09:19
Excellent! Works great and has helped me a lot. Thanks.
praveen kumar N     01 Jun 2016, 05:52
I like this code. this is helps lot to me . now I am starting my career in coding thank you so much.
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2017 Sharon Parq Associates, Inc.