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.
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").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.
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!
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
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
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."
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.