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.
Note:
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 MoreWhen you assign a macro to a shortcut key, you make it easy to run the macro without ever removing your hands from the ...
Discover MoreNeed to hide some macros in your workbook? There are three ways you can do it, as covered in this discussion.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
@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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments