Create Multiple Files using Template with Files Names in a Column List

Kreme

New Member
Joined
Jul 11, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello!

The code below continues to error out at the section below. Any thing I can use to determine why? Or adjustments I can make to the code?

VBA Code:
wkbTemplate.SaveAs strSavePath & rng.Value



VBA Code:
Public Sub SaveTemplate()
  Const strSavePath As String = "C:\My Documents\"
  Const strTemplatePath As String = "C:\My Documents\template.xls"

  Dim rngNames As Excel.Range
  Dim rng As Excel.Range
  Dim wkbTemplate As Excel.Workbook
  
  Set rngNames = ThisWorkbook.Worksheets("Name Index").Range("A1:A100").Values
  
  Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)
  
  For Each rng In rngNames.Cells
     wkbTemplate.SaveAs strSavePath & rng.Value
  Next rng
  
  wkbTemplate.Close SaveChanges:=False
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi Kreme,

This worked for me:

VBA Code:
Option Explicit
Public Sub SaveTemplate()

  Const strSavePath As String = "C:\My Documents\"
  Const strTemplatePath As String = "C:\My Documents\template.xls"

  Dim rngNames As Range
  Dim rng As Range
  Dim wkbTemplate As Workbook
  
  Application.ScreenUpdating = False
  
  Set rngNames = ThisWorkbook.Worksheets("Name Index").Range("A1:A" & ThisWorkbook.Worksheets("Name Index").Cells(Rows.Count, "A").End(xlUp).Row) 'Finds the last used row in Col. A
  
  Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)
  
  For Each rng In rngNames
    Application.DisplayAlerts = False 'Automatically overwrite if the file already exists no questions asked
        wkbTemplate.SaveAs strSavePath & rng.Value, FileFormat:=56 '56 = xlExcel8 (97-2003 format in Excel 2007-2016, xls) https://www.rondebruin.nl/win/s5/win001.htm
    Application.DisplayAlerts = True
  Next rng
  
  wkbTemplate.Close SaveChanges:=False
  
  Application.ScreenUpdating = True
  
End Sub

Just make sure that all the proposed names in Col. A are valid workbook names i.e. no blanks and no illegal characters like [

Regards,

Robert
 
Upvote 0
Hi Kreme,

This worked for me:

VBA Code:
Option Explicit
Public Sub SaveTemplate()

  Const strSavePath As String = "C:\My Documents\"
  Const strTemplatePath As String = "C:\My Documents\template.xls"

  Dim rngNames As Range
  Dim rng As Range
  Dim wkbTemplate As Workbook
 
  Application.ScreenUpdating = False
 
  Set rngNames = ThisWorkbook.Worksheets("Name Index").Range("A1:A" & ThisWorkbook.Worksheets("Name Index").Cells(Rows.Count, "A").End(xlUp).Row) 'Finds the last used row in Col. A
 
  Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)
 
  For Each rng In rngNames
    Application.DisplayAlerts = False 'Automatically overwrite if the file already exists no questions asked
        wkbTemplate.SaveAs strSavePath & rng.Value, FileFormat:=56 '56 = xlExcel8 (97-2003 format in Excel 2007-2016, xls) https://www.rondebruin.nl/win/s5/win001.htm
    Application.DisplayAlerts = True
  Next rng
 
  wkbTemplate.Close SaveChanges:=False
 
  Application.ScreenUpdating = True
 
End Sub

Just make sure that all the proposed names in Col. A are valid workbook names i.e. no blanks and no illegal characters like [

Regards,

Robert
Hello,
I receive a Runtime error 9 : Subscript out of range.

Anybody has any idea what that might be?

Thank you.
 
Upvote 0
I receive a Runtime error 9 : Subscript out of range.
Anybody has any idea what that might be?

I take it that this line is causing the error:

VBA Code:
Set rngNames = ThisWorkbook.Worksheets("Name Index").Range("A1:A" & ThisWorkbook.Worksheets("Name Index").Cells(Rows.Count, "A").End(xlUp).Row)

If so the reason is because that you don't have a sheet in the workbook exactly called Name Index. Change the two references in the above code to the sheet name you're using to store the file name and and try again.
 
Upvote 0
Thank you.
Seemed to have worked, but I receive a different error : Runtime error 1004 : Application defined or object defined error, apparently on this line :

Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)
 
Upvote 0
I receive a different error : Runtime error 1004 : Application defined or object defined error

Change the strTemplatePath variable to the exact path where the template you want to use resides. Also if the template is not in xls format you'll need to chnage the 56 in this line to suit:

VBA Code:
wkbTemplate.SaveAs strSavePath & rng.Value, FileFormat:=[B]56[/B]

The file names in Col. A need to have the relevant extension as well like Red.xlsx, Green.xlsx and Blue.xlsx
 
Upvote 0
Thank you, Trebor. It worked perfectly after implementing your recommendations.
 
Upvote 0
Thank you, Trebor. It worked perfectly after implementing your recommendations.

Thanks for letting us know and you're welcome 😎
 
Upvote 0

Forum statistics

Threads
1,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

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