VBA to duplicate a file in folder by n times

chasoe

Board Regular
Joined
Jan 29, 2006
Messages
73
Hi,

I just come across a problem on how use VBA can simulate in window explorer, to copy and paste a specific file in same folder as many times as the user may require.

Without the codes, I've to click this specific file (say abc.xlsx) in window explorer, and press Ctrl + C, and then press Ctrl + V 45 times in order to make 45 copies of the original abc.xlsx,
ie
abc.xlsx
abc - copy.xlsx
abc - copy (2).xlsx
abc - copy (3).xlsx
.......
abc - copy (44).xlsx

The reason to make so many copies is that I'll have another set of codes (searched from internet) to list out the folder files and rename them one by one before despatching them to 45 recipients by emails for their further update of the content in the file.

As I find it too clumsy, I think there should be codes that can directly ask user how many copies of the existing file in the folder to make and then proceed accordingly.

See if anyone could kindly help.

Many thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi,

Please check below code:

VBA Code:
Sub copyFiles()
Dim folderPath As String
Dim copyReqd As Integer, copyCount As Integer

folderPath = "C:\folderpath\"

If Dir(folderPath) = vbNullString Then
    MsgBox "No file available"
    Exit Sub
End If

copyReqd = CInt(InputBox("How many copy required of " & Dir(folderPath), "Enter Duplicate count"))

For copyCount = 1 To copyReqd
    FileCopy folderPath & Dir(folderPath), folderPath & "copy" & copyCount & ".xlsx"
Next

MsgBox copyReqd & " copies created successfully."

End Sub
 
Upvote 0
@chasoe how about:

VBA Code:
Sub DuplicateFile()
'
    Dim DuplicateNumber     As Long, NumberOfDuplicates As Long
    Dim FileNameOnly        As String, FilePath         As String, FileToCopy   As String
'
    FileToCopy = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Please select the file that you want to make copies of!")
'
    If FileToCopy = "False" Then
        Exit Sub
    Else
        Workbooks.Open Filename:=FileToCopy
    End If
'
    FilePath = Left(FileToCopy, InStrRev(FileToCopy, "\"))
    FileNameOnly = Mid(ActiveWorkbook.Name, 1, InStr(1, ActiveWorkbook.Name, ".") - 1)
'
    NumberOfDuplicates = InputBox("How many copies of the active workbook do you want to make?")
'
    If NumberOfDuplicates >= 1 Then
        For DuplicateNumber = 1 To NumberOfDuplicates
            If DuplicateNumber = 1 Then
                ActiveWorkbook.SaveCopyAs FilePath & FileNameOnly & " - copy" & ".xlsx"
            Else
                ActiveWorkbook.SaveCopyAs FilePath & FileNameOnly & " - copy(" & DuplicateNumber & ")" & ".xlsx"
            End If
        Next
   End If
'
    ActiveWorkbook.Close savechanges:=False
End Sub
 
Upvote 0
@Saurabhj Thank you
@johnnyL Thank you

Both set of codes have been tested OK, I'll see which one best suit my situation and modify a bit, and incorporate into my other codes to make them complete.

Many thanks indeed.
 
Upvote 0
@chasoe how about:

VBA Code:
Sub DuplicateFile()
'
    Dim DuplicateNumber     As Long, NumberOfDuplicates As Long
    Dim FileNameOnly        As String, FilePath         As String, FileToCopy   As String
'
    FileToCopy = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Please select the file that you want to make copies of!")
'
    If FileToCopy = "False" Then
        Exit Sub
    Else
        Workbooks.Open Filename:=FileToCopy
    End If
'
    FilePath = Left(FileToCopy, InStrRev(FileToCopy, "\"))
    FileNameOnly = Mid(ActiveWorkbook.Name, 1, InStr(1, ActiveWorkbook.Name, ".") - 1)
'
    NumberOfDuplicates = InputBox("How many copies of the active workbook do you want to make?")
'
    If NumberOfDuplicates >= 1 Then
        For DuplicateNumber = 1 To NumberOfDuplicates
            If DuplicateNumber = 1 Then
                ActiveWorkbook.SaveCopyAs FilePath & FileNameOnly & " - copy" & ".xlsx"
            Else
                ActiveWorkbook.SaveCopyAs FilePath & FileNameOnly & " - copy(" & DuplicateNumber & ")" & ".xlsx"
            End If
        Next
   End If
'
    ActiveWorkbook.Close savechanges:=False
End Sub



HI, is it possible to do this for all type of files instead of just excel workbook?
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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