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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

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,872
Messages
5,489,411
Members
407,687
Latest member
NeoSez

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top