VBA: Copy different sheet in same workbook multiple times, naming according to list in cells

RichCowell

Board Regular
Joined
Dec 5, 2013
Messages
121
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've found a VBA script to duplicate the current active worksheet, but I need to copy a different worksheet in the workbook X amount of times (X = number in a cell).

e.g.
Active Worksheet Name: Start
Worksheet to be copied: Master
Copied X times where X = Start!A10
Names of worksheets: Start!B10:B25

Can anyone help me out? I had assumed it would need to be done in two stages - replicating the Master sheet, then renaming them - but I was going to (try to) cross that bridge when I got the first bit done.

Thanks,

Rick
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
So you want to make several copies of your sheet named "Master"
And the number of times you want it copied Will be found in sheet named "Start" Range("A10")

Now how do we know what to name these sheets?

You show:
Names of worksheets: Start!B10:B25

But that would mean 16 sheet

How about B1 to last filled in cells in column B

So if you had:
On sheet Start
B1 Alpha
B2 Bravo
B3 Charlie

The script would know you want sheet master copied 3 times and name the sheets Alpha Bravo Charlie

Would that work ?
 
Upvote 0
Yes, that would give the same result so should work nicely, thank you :-)... Can you help?
 
Last edited:
Upvote 0
Try this:
Code:
Sub Copy_Master()
'Modified 7/22/2019 5:55:33 AM  EDT
On Error GoTo M
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheets("Start").Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To Lastrow
    Sheets("Master").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = Sheets("Start").Cells(i, "B").Value
Next
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "That sheet name may already be used or you made some mistake"
End Sub
 
Upvote 0
My script assumes:
1. You have a sheet named: "Master"
2.You have a sheet named "Start"
3. You have names entered in Sheet "Start" Columns B starting in Range"B1"
 
Upvote 0
That seems to work perfectly, thank you!
There was a slight issue as I had a named range called "Picker" that it didn't like for some reason, that kept sending Excel into a loop and I had to crash it, I wasn't use it any more so just deleted it and it worked straight away...
Thank you!
 
Upvote 0
Quick follow-up...
Is there any that can work with the "Master" worksheet being hidden? The script does run, but not correctly.
 
Upvote 0
I never tested to see if a named Range would be a problem.
Are you saying the sheet named Master is hidden and that is causing a problem?

Is it hidden or very hidden?

I never tested that but I can see how that could be a problem. Some people like hiding sheets so others cannot see them. Writing a script to copy a hidden sheet may cause a problem I have never tried that.
 
Upvote 0
Not sure what this means:
The script does run, but not correctly.
What does it do wrong?
 
Upvote 0
I never tested to see if a named Range would be a problem.
Are you saying the sheet named Master is hidden and that is causing a problem?

Is it hidden or very hidden?

I never tested that but I can see how that could be a problem. Some people like hiding sheets so others cannot see them. Writing a script to copy a hidden sheet may cause a problem I have never tried that.

I wasn't aware there were more ways to hide them - I'd just right clicked on the tab and clicked Hide.

Could the VBA be adapted to unhide "Master" at the start, then hide it again at the end?

Not sure what this means:
The script does run, but not correctly.
What does it do wrong?

It kept giving me a message about renaming, but when I entered a different name it just gave me the same message - don't worry about this one - deleting the range solved it.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top