Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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 and Naming a Worksheet Using a Macro.

Creating and Naming a Worksheet Using a Macro

by Allen Wyatt
(last updated December 13, 2019)


Jeff would like to create a copy of his "master" worksheet, prompt for a name of the new worksheet, and move it to the end of the worksheet tabs, all from within a macro. He tried to record a macro to do this, but it didn't work.

The fact that the recorded macro didn't work isn't terribly surprising. When you record a macro, you tell Excel to record the steps you take. Those steps (in this instance) included the naming of the worksheet, so that name was recorded in the macro. Try to run the macro a second time, and you will get an error because the worksheet you are trying to create on the second pass was already created on the first.

In this case you have to write a macro manually. You can start with recording the process, and you will get a code like the following:

Sub Macro1()
    Sheets("Master").Copy After:=Sheets(3)
    Sheets("Master (2)").Select
    Sheets("Master (2)").Name = "NewMaster"
End Sub

Note that the code places the worksheet (after the third sheet) and then always names it the same thing. There's a lot to change here. What you want to do is change it to something like the following:

Sub CopyRename()
    Dim sName As String
    Dim wks As Worksheet
    Worksheets("Master").Copy after:=Sheets(Worksheets.Count)
    Set wks = ActiveSheet
    Do While sName <> wks.Name
        sName = Application.InputBox _
          (Prompt:="Enter new worksheet name")
        On Error Resume Next
        wks.Name = sName
        On Error GoTo 0
    Set wks = Nothing
End Sub

This macro will copy the worksheet named "Master" to the end of sheet list (no matter how many sheets you have in the workbook) and continue to prompt for a new worksheet name until a valid name is entered.


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 (11929) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Creating and Naming a Worksheet Using 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. ...


One Change Affects Everything

Have you ever made one formatting change in your document, only to see that change applied to all the paragraphs in the ...

Discover More

Counting the Number of Blank Cells

If you need to count the number of blank cells in a range, the function to use is COUNTBLANK. This tip discusses the ...

Discover More

Pasting Clean Text

One of the most helpful tools in Word is the ability to paste straight text into a document. This is used so much on my ...

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)

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

Discover More

Removing a Macro from a Shortcut Key

When you assign a macro to a shortcut key, you make it easy to run the macro without ever removing your hands from the ...

Discover More

Hiding Macros

Need to hide some macros in your workbook? There are three ways you can do it, as covered in this discussion.

Discover More

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

View most recent newsletter.


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 five minus 3?

2017-01-29 07:04:54

Robert H. Woodman


Thanks for your response. I will fully write out my requirements and then consider whether I want to tackle this (it may be beyond my current skill level) or outsource it.

Again, thank you.

2017-01-28 05:27:04



The short answer is "yes" it can be done in VBA but it is non-trivial task.

You would use, I would suggest, a Userform to get the input data from the User, create the required worksheets, then create the links to the Summary sheet so once the data on the individual samples has been entered it will automatically appear on the Summary sheet.

As with any User input it is wise to 'validate' it before actioning it.

This will be several hours work once it is fully defined what the requirements are.

2017-01-27 14:30:37

Robert H. Woodman

This is an excellent tip. Thank you. I have a similar, but more complicated request.

I work in quality control. For a particular product, I have a very specific method of determining purity; however, depending on how the batch was made, the batch may be split into multiple containers. Four samples will be taken from each container for analysis. Each sample will have its analytical data on its own worksheet. There is a sheet that summarizes the data and gives the mean, standard deviation, and residual standard deviation of all the purity data.

I have a master worksheet for each sample's analytical data, and I have the summary worksheet both in the same workbook. What I'd like to do is have a form pop up that asks (a) how many bottles are being analyzed and (b) how many samples from each bottle (min 4). From the data entered on the form, Excel would then create copies of the sample's analytical worksheet. The copies would be named "Bottle 1, Sample 1", "Bottle 1, Sample 2", and so forth. The master worksheet would then be modified in appropriate places so that the Mean, Std. Dev., and %RSD would be analyzed from all samples.

Is there a way to do all of those things in Excel 2010 VBA?

2016-04-30 05:59:47

Willy Vanhaelen

With the second macro, if you change your mind and press cancel, you still get a new window named "False".

Here is a version of the macro that deals with it. It will ask to enter a name for the new worksheet until you enter a valid one unless you press cancel, it then will delete the just created sheet and end.

Sub Macro3()
Dim sName As String
Sheets("Master").Copy After:=Sheets(Sheets.Count)
On Error Resume Next
sName = InputBox("Enter new valid worksheet name")
If sName = "" Then
Application.DisplayAlerts = False
Exit Sub
End If
ActiveSheet.Name = sName
If ActiveSheet.Name = sName Then Exit Do
End Sub

If you replace
Worksheets("Master").Copy after:=Sheets(Worksheets.Count)
ActiveSheet.Copy after:=Sheets(Worksheets.Count)
You can make a copy of any sheet. Just make it active and run the macro.

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

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.