Copy a Workbook Multiple Time and Rename based on a list

mdpw

New Member
Joined
Apr 8, 2022
Messages
1
Office Version
  1. 2010
Platform
  1. Windows
Hello,

I need to duplicate a workbook multiple times and rename the new workbook based on a list of names. I found this code and it worked once but now I get an error. The workbook does contain multiple sheets.

Sub SaveMasterAs()
Dim wb As Workbook
Dim rNames As Range, c As Range, r As Range
Set rNames = Worksheets("Sheet1").Range("A2", Worksheets("Sheet1").Range("A2").End(xlDown))
Set wb = Workbooks.Open(ThisWorkbook.Path & "\Template.xlsx")
For Each c In rNames
With wb
'.Worksheets("Sheet1").Range("A1").Value = c.Offset(, 1).Value 'ID
'Path and name for copied workbook
.SaveAs Filename:=ThisWorkbook.Path & "\Template Copy\" & c.Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End With
Set wb = ActiveWorkbook
Next c
wb.Close
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi and welcome to MrExcel.

It is necessary to open the file or copying the file with the new names is enough:
VBA Code:
Sub CopyTemplate()
  Dim c As Range
  For Each c In Sheets("Sheet1").Range("A2", Sheets("Sheet1").Range("A" & Rows.Count).End(3))
    FileCopy ThisWorkbook.Path & "\Template.xlsx", ThisWorkbook.Path & "\Template copy " & c.Value & ".xlsx"
  Next c
End Sub
 
Upvote 0
Just tested the code above and it says bad file name or number?
 
Upvote 0
VBA Code:
Sub addnewworkbook()
Set template = ActiveWorkbook
Set namelist = Workbooks("Name List.xlsm")
  namelist.Activate
  Dim c As Range
  Dim targetworkbookpath As Variant
  targetworkbookpath = Range("F1").Value
  For Each c In Sheets("Test").Range("A2", Sheets("Test").Range("A" & Rows.Count).End(3))
    FileCopy template, targetworkbookpath & c.Value & ".xlsx"
  Next c

End Sub

Thank you for your quick response! Just edited the code and it's saying object doesn't support this property on the filecopy line. I wonder if template has to be closed before it's copied?
 
Upvote 0
VBA Code:
Sub addnewworkbook()
Dim template As String
template = "C:\Users\Template_ ABC_2022-03-31"
Set namelist = Workbooks("ABC Name List.xlsm")
  namelist.Activate
  Dim c As Range
  Dim targetworkbookpath As String
  targetworkbookpath = Range("F1").Value
  For Each c In Sheets("Test").Range("A2", Sheets("Test").Range("A" & Rows.Count).End(3))
    FileCopy template, targetworkbookpath & c.Value & ".xlsx"
  Next c

End Sub

Just edited the code. It seems like it can't copy opened workbook. Now it's saying file not found on the filecopy row...
 
Upvote 0
template = "C:\Users\Template_ ABC_2022-03-31"
Check that the folder and name are spelled correctly. The file extension is missing.

targetworkbookpath = Range("F1").Value
What you have in cell F1, you need to be even more specific. At the end of the data in cell F1 do you have the Backslash "\"?

Range("A2"
An example of the data you have in cell A2.

namelist.Activate
And the active sheet of your book is the "Test" sheet?
 
Upvote 0
Check that the folder and name are spelled correctly. The file extension is missing.


What you have in cell F1, you need to be even more specific. At the end of the data in cell F1 do you have the Backslash "\"?


An example of the data you have in cell A2.


And the active sheet of your book is the "Test" sheet?
Good points;) Let me go back and check.
 
Upvote 0
Check that the folder and name are spelled correctly. The file extension is missing.
added extension
What you have in cell F1, you need to be even more specific. At the end of the data in cell F1 do you have the Backslash "\"?
path to the folder that I want the copies to be in
An example of the data you have in cell A2.
List of product names
Duland Village
And the active sheet of your book is the "Test" sheet? - that's a good question. There is only one sheet in the file - a dummy file to make copies of the template. But it potentially can be a problem.

VBA Code:
Sub addnewworkbook()
Dim template As String
template = "C:\Macro\ABC\Template - ABC_2022-03-31.xlsx" ' added extension
Set namelist = Workbooks("ABC Name List.xlsm") 
  namelist.Activate ' it's a good question to ask if test is the active sheet. There is only one sheet so it wasn't a problem. But it could be. 
  Dim c As Range
  Dim targetworkbookpath As String
  targetworkbookpath = Range("F1").Value ' filepath to the target folder. added "\" to the end of the file path
  For Each c In Sheets("Test").Range("A2", Sheets("Test").Range("A" & Rows.Count).End(3))
    FileCopy template, targetworkbookpath & c.Value & ".xlsx"
  Next c

End Sub

I just tried the code above and it worked!!! Thank you soooo much!!!
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,210
Members
449,090
Latest member
bes000

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