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

dmdek

New Member
Joined
May 3, 2011
Messages
4
Could someone please assist with a problem I cannot think through. I want to use 1 template file and then "save as" renaming the file 200+.

The file names will pick up their names from a column list. I was hoping the macro will have a variable so that I can indicate where the files should be stored.

I cannot figure out how to loop through the cells in the column to pick up the file names.


Thank you in advance.

Dan



Excel 2003
 
That's what .Offset(0, 1) does - offset 0 rows, 1 column. So if rng is A2, then rng.Offset(0,1) is B2.
It shows following error

1623428737935.png


1623428831837.png




What to be done?
 

Attachments

  • 1623428766397.png
    1623428766397.png
    21.3 KB · Views: 15
  • 1623428807113.png
    1623428807113.png
    20.4 KB · Views: 14
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Oh, my bad. It needs a sheet. Will the worksheet with H16 always be the same?

For example,
Code:
wkbTemplate.Worksheets("Sheet1").Range("H16") = rng.Offset(0, 1).Value
or if it's always the first sheet
Code:
wkbTemplate.Worksheets(1).Range("H16") = rng.Offset(0, 1).Value

something along those lines.
 
Upvote 0
I have followed the code and it works for about 7 files then stops bugging our at the saveAs part, any ideas?

TIA
 
Upvote 0
Sorry, it's been a while. Can you post the version of the code that you're using, as there have been multiple iterations above.

Also, when you saying "bugging" does it display an error message, or something else happens (e.g. freeze, crash)? If you get an error also post that.
 
Upvote 0
Thanks for your response

I used the original one that you posted years ago. However, I found them all this morning in My Documents which was not the path I had in the code. I will faff with it and see if I can get it to create them where they should be.
 
Upvote 0
I got it to work and it is brilliant, this is what I am using

Public Sub CreateFiles()

Const StrSavePath As String = "C:\Users\Amorr\OneDrive\Personal\iGCSE 4IT1_02\2306\My Allocation\94044"
Const strTemplatePath As String = "C:\Users\Amorr\OneDrive\Personal\iGCSE 4IT1_02\2306\My Allocation\4IT_02_Calculated_Marking_Spreadsheet_2306 New Version 3.xlsm"


Dim rngNames As Excel.Range
Dim wkbTemplate As Excel.Workbook
Dim rng As Excel.Range


Set rngNames = ThisWorkbook.Worksheets("Centre Number").Range("A10:A12")
Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)

For Each rng In rngNames.Cells

wkbTemplate.SaveAs StrSavePath & "\" & "4IT_02_" & rng.Value & "_531696"

Next rng

wkbTemplate.Close SaveChanges:=False

End Sub

However, I have a favour the tail end of the save path (94044) changes depending on the centre number, also the range changes depending on the length of the list, any ideas how to sort this out please?
 
Upvote 0
Depending on how the centre number changes, but you probably need to parametrize that portion of the path. In other words, if you have:

VBA Code:
Const StrSavePath As String = "C:\Users\Amorr\OneDrive\Personal\iGCSE 4IT1_02\2306\My Allocation\"

and then if your centre number is to the right of your file name portion (i.e. B10:B12) you might have something like this in the loop:

VBA Code:
For Each rng In rngNames.Cells

  wkbTemplate.SaveAs StrSavePath & rng.Offset(0, 1).Value & "\" & "4IT_02_" & rng.Value & "_531696"

Next rng


There is a number of ways you can approach a variable length list, for instance if it always starts in A10, you could use:
VBA Code:
With ThisWorkbook.Worksheets("Centre Number")
  Set rngNames = Range(.Range("A10"), .Range("A10").End(xlDown))
End With

You might also consider a dynamic named range, using a Table (i.e. structured range), etc. If using a table, you'd have to modify the code slightly using a ListObject data type.
 
Upvote 0
Thank you so much. I did get it working using the range method and it was so much easier. Thank you for your help
 
Upvote 0

Forum statistics

Threads
1,214,899
Messages
6,122,155
Members
449,068
Latest member
shiz11713

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