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

RichCowell

Board Regular
Joined
Dec 5, 2013
Messages
98
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
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
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 ?
 

RichCowell

Board Regular
Joined
Dec 5, 2013
Messages
98
Yes, that would give the same result so should work nicely, thank you :)... Can you help?
 
Last edited:

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
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"
 

RichCowell

Board Regular
Joined
Dec 5, 2013
Messages
98
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!
 

RichCowell

Board Regular
Joined
Dec 5, 2013
Messages
98
Quick follow-up...
Is there any that can work with the "Master" worksheet being hidden? The script does run, but not correctly.
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
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.
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
Not sure what this means:
The script does run, but not correctly.
What does it do wrong?
 

RichCowell

Board Regular
Joined
Dec 5, 2013
Messages
98
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,378
Messages
5,486,519
Members
407,550
Latest member
LucasBordure

This Week's Hot Topics

Top