Create multiple files from template

ahmedmohamedkamal

New Member
Joined
Dec 28, 2019
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
Hi every one
I have a code edited from old thread that create new excel files based on template and named after specified range in the master sheet
i have a problem that it copy the first cell A1 in every C6 for all generated files
i need A1 copied to C6 1st file and A2 copied to C6 2nd file ...........etc also i need to make the range dynamic according to used cells no just from a1 to a8
The code is
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("Sheet1").Range("A1:A8").Values

  Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)

  For Each rng In rngNames.Cells
    wkbTemplate.Worksheets("Sheet1").Range("C6").Value = ThisWorkbook.Worksheets("Sheet1").Range("A1:A8").Value
    wkbTemplate.SaveAs strSavePath & rng.Value
  Next rng

  wkbTemplate.Close SaveChanges:=False
End Sub
 
Last edited by a moderator:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi & welcome to MrExcel.
How about
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

With ThisWorkbook.Worksheets("Sheet1")
    Set rngNames = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
End With
Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)

For Each rng In rngNames.Cells
wkbTemplate.Worksheets("Sheet1").Range("C6").Value = rng.Value
wkbTemplate.SaveAs strSavePath & rng.Value
Next rng

wkbTemplate.Close SaveChanges:=False
End Sub
 
Upvote 0
Thank you very much for your quick reply this work like a charm
I need to copy other cells from mater sheet to generated files (All full cells of column C4 copied to C14 ) and ( Only H2 master sheet copied to L6 copied files )
sorry if this is to much to ask my friend.

Hi & welcome to MrExcel.
How about
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

With ThisWorkbook.Worksheets("Sheet1")
    Set rngNames = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
End With
Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)

For Each rng In rngNames.Cells
wkbTemplate.Worksheets("Sheet1").Range("C6").Value = rng.Value
wkbTemplate.SaveAs strSavePath & rng.Value
Next rng

wkbTemplate.Close SaveChanges:=False
End Sub
 
Upvote 0
How about
VBA Code:
Public Sub SaveTemplate()
Const strSavePath As String = "C:\My Documents\"
Const strTemplatePath As String = "C:\My Documents\template.xls"


Dim rngNames As Range, rngC As Range
Dim rng As Range
Dim wkbTemplate As Workbook

With ThisWorkbook.Worksheets("Sheet1")
    Set rngNames = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
    Set rngC = .Range("C4", .Range("C" & Rows.Count).End(xlUp))
End With
Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)

For Each rng In rngNames.Cells
    With wkbTemplate.Worksheets("Sheet1")
        .Range("C6").Value = rng.Value
        .Range("C14").Resize(rngC.Count).Value = rngC.Value
        .Range("L6").Value = thisworkbbok.Sheets("Sheet1").Range("H2").Value
    End With

wkbTemplate.SaveAs strSavePath & rng.Value
Next rng

wkbTemplate.Close SaveChanges:=False
End Sub
 
Upvote 0
How about
VBA Code:
Public Sub SaveTemplate()
Const strSavePath As String = "C:\My Documents\"
Const strTemplatePath As String = "C:\My Documents\template.xls"


Dim rngNames As Range, rngC As Range
Dim rng As Range
Dim wkbTemplate As Workbook

With ThisWorkbook.Worksheets("Sheet1")
    Set rngNames = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
    Set rngC = .Range("C4", .Range("C" & Rows.Count).End(xlUp))
End With
Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)

For Each rng In rngNames.Cells
    With wkbTemplate.Worksheets("Sheet1")
        .Range("C6").Value = rng.Value
        .Range("C14").Resize(rngC.Count).Value = rngC.Value
        .Range("L6").Value = thisworkbbok.Sheets("Sheet1").Range("H2").Value
    End With

wkbTemplate.SaveAs strSavePath & rng.Value
Next rng

wkbTemplate.Close SaveChanges:=False
End Sub

Perfect
I can't thank you enough
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Sorry man but i don't understand how the loop work i tried many times to edit the code to copy G4 master sheet to F14 1st generated file and G5 to second ......etc
but i cant do this can you help please
 
Upvote 0
Is there anything else that needs to be copied, or is this the final change?
 
Upvote 0
Ok, add this line
Rich (BB code):
    With wkbTemplate.Worksheets("Sheet1")
        .Range("C6").Value = rng.Value
        .Range("F15").Value = rng.Offset(3, 6).Value
        .Range("C14").Resize(rngC.Count).Value = rngC.Value
        .Range("L6").Value = thisworkbbok.Sheets("Sheet1").Range("H2").Value
    End With
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

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