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 April 30, 2016)

4

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").Select
    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
    Loop
    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.

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. ...

MORE FROM ALLEN

Can't Use Hyperlinks

Before some features in Excel can function properly, you must have the correct permissions set for the user of the computer. ...

Discover More

Resizing Checkboxes

If you create a user form in VBA that includes checkboxes, you may want to make the checkboxes larger. You can't adjust their ...

Discover More

Deleting All Fields

Fields can be a great way of adding small snippets of dynamic data to your documents. However, you may want to get rid of all ...

Discover More

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!

More ExcelTips (ribbon)

Determining How Many Windows are Open

Does your macro need to know how many windows Excel has open? You can determine it by using the Count property of the Windows ...

Discover More

Saving Information in a Text File

The VBA programming language provide with Excel allows you to create and modify text files quite easily. Here's how to open a ...

Discover More

Creating a Directory in a Macro

One of the things you can do with macros is to work with disk files. As you do so, you may have a need to create a new ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. 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 6 - 4?

2017-01-29 07:04:54

Robert H. Woodman

@Barry

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

Barry

@Robert

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
Do
sName = InputBox("Enter new valid worksheet name")
If sName = "" Then
Application.DisplayAlerts = False
ActiveSheet.Delete
Exit Sub
End If
ActiveSheet.Name = sName
If ActiveSheet.Name = sName Then Exit Do
Beep
Loop
End Sub

If you replace
Worksheets("Master").Copy after:=Sheets(Worksheets.Count)
with
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
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.