VBA to copy the master file workbook 500 times with different file names

chris1979

Board Regular
Joined
Feb 23, 2016
Messages
52
Hi Guys
Could you please assist me

I have a master workbook that needs to be copied 500 times with different file names based on a list.

I have used the following code

Sub CopyAndRenameFiles_3()

' Define variables
Dim MasterFilePath As String
Dim NewFilePath As String
Dim FileListFile As String
Dim FileListSheet As String
Dim FileListRange As String
Dim i As Integer
Dim NewFolder As String
Dim FileList() As String
Dim FileNumber As Integer
Dim FileLine As String

' Set the path to the master file
MasterFilePath = "C:\Users\C.Terrence\Downloads\SourceFiles\Masterfile.xlsm"

' Set the path to the file containing the list of file names
FileListFile = "C:\Users\C.Terrence\Downloads\Newfile\FileList.xlsx"

' Set the sheet name and range containing the list of file names
FileListSheet = "Sheet1"
FileListRange = "A1:A3" ' Assumes the file names are in cells A1 to A3

' Set the path to the new folder
NewFolder = "C:\Users\C.Terrence\Downloads\New\"

' Create the new folder if it doesn't exist
If Dir(NewFolder, vbDirectory) = "" Then
MkDir NewFolder
End If

' Open the file containing the list of file names
FileNumber = FreeFile()
Open FileListFile For Input As #FileNumber

' Loop through each line in the file and add it to the FileList array
i = 0
While Not EOF(FileNumber)
Line Input #FileNumber, FileLine
ReDim Preserve FileList(i)
FileList(i) = FileLine
i = i + 1
Wend

' Close the file
Close #FileNumber

' Loop through the list of file names and copy the master file
For i = LBound(FileList) To UBound(FileList)

' Define the new file path
NewFilePath = NewFolder & FileList(i)

' Copy the master file to the new file path
On Error Resume Next
FileCopy MasterFilePath, NewFilePath
If Err.Number <> 0 Then
MsgBox "Error " & Err.Number & ": " & Err.Description & " for file " & NewFilePath
End If
On Error GoTo 0

Next i

End Sub

But I am getting an error " Bad file name or number" in line "FileCopy MasterFilePath, NewFilePath"

Please assist

CT
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
have you added a watch to the NewFilePath to see what it contains

or add

debug.print NewFilePath
stop

after its been defined to view it in the immediate window
 
Upvote 0
Thank you for the reply

Attached is the screenshot of the error.
 

Attachments

  • Untitled-min.jpg
    Untitled-min.jpg
    72.2 KB · Views: 9
Upvote 0
try the below just change the paths [ C:\Users\Public\Documents\ ] to suit your paths

VBA Code:
Sub CopyAndRenameFiles_3()

' Define variables
Dim MasterFilePath As String
Dim NewFilePath As String
Dim FileListFile As String
Dim FileListSheet As String
Dim FileListRange As String
Dim NewFolder As String
Dim FileList() As String
'Dim FileNumber As Integer
'Dim FileLine As String
Dim flst As Workbook
Dim rng As Range, cel As Range

' Set the path to the master file
MasterFilePath = "C:\Users\Public\Documents\Masterfile.xlsx"

' Set the path to the file containing the list of file names
FileListFile = "C:\Users\Public\Documents\FileList.xlsx"

' Set the sheet name and range containing the list of file names
FileListSheet = "Sheet1"
FileListRange = "A2:A16" ' Assumes the file names are in cells A1 to A16

' Set the path to the new folder
NewFolder = "C:\Users\Public\Documents\New\"

' Create the new folder if it doesn't exist
If Dir(NewFolder, vbDirectory) = "" Then
MkDir NewFolder
End If

' Open the file containing the list of file names
Set flst = Workbooks.Open(FileListFile)

Set rng = flst.Sheets(FileListSheet).Range(FileListRange)

' Loop through each line in the file and save the main file
For Each cel In rng

' Define the new file path
NewFilePath = NewFolder & cel

' Copy the master file to the new file path
On Error Resume Next
FileCopy MasterFilePath, NewFilePath
If Err.Number <> 0 Then
MsgBox "Error " & Err.Number & ": " & Err.Description & " for file " & NewFilePath
End If
On Error GoTo 0

Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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