Save multiple files in the same folder as copies with Same Name with Word "Copy" ?

MrSpark

New Member
Joined
Mar 1, 2021
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
I have been using below code which create the copy of same workbook in the same folder with same name "Copy".

What i want is I need that code in a way, Where i will run the code it will ask me to select a folder after selecting the folder which have multiple workbooks and also on their sub folder. It should run and create copies. that way my huge time will saved.

Any help will be much appreciated.


VBA Code:
Dim DISTNAME As String

DISTNAME = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & "Copy" & ".xlsx"
DISTNAME = ActiveWorkbook.Path & Application.PathSeparator & DISTNAME

Sheets.Select
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
ActiveWorkbook.SaveAs DISTNAME
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
See if the following works for you (please test it on a non-critical folder first):
Excel Formula:
Sub CopyAll()
    Dim f As FileDialog, s As String
    Set f = Application.FileDialog(msoFileDialogFolderPicker)
    With f
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then Set f = Nothing: Exit Sub
        s = .SelectedItems(1)
    End With
    Set f = Nothing
    s = """for /R " & s & " %A in (*.xl*) do copy ""%A"" ""%~dpnA - Copy%~xA"""""
    Shell "cmd /c " & s
End Sub
 
Upvote 0
I have this folder with 5 files and run the code but nothing happens.

1633768745512.png
 
Upvote 0
Hi, a VBA demonstration as a beginner starter :​
VBA Code:
Sub Demo1()
      Const E = ".xlsx"
        Dim P$, F$
    With Application.FileDialog(4)
        If .Show Then P = .SelectedItems(1) & "\" Else Exit Sub
    End With
          F = Dir$(P & "*" & E)
    While F > ""
          FileCopy P & F, P & Replace(F, E, " Copy " & E)
          F = Dir$
    Wend
End Sub
 
Upvote 0
I have this folder with 5 files and run the code but nothing happens.
Oops, I have missed a couple of double quotes. Please try the corrected version:
VBA Code:
Sub CopyAll()
    Dim f As FileDialog, s As String
    Set f = Application.FileDialog(msoFileDialogFolderPicker)
    With f
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then Set f = Nothing: Exit Sub
        s = .SelectedItems(1)
    End With
    Set f = Nothing
    s = """for /R """ & s & """ %A in (*.xl*) do copy ""%A"" ""%~dpnA - Copy%~xA"""""
    Shell "cmd /c " & s
End Sub
 
Upvote 0
Solution
Thank you very much Sir one more last favor. If i want to delete those workbooks which copies have been generated with above code. then how to achieve this.
 
Upvote 0
You are welcome.

Here is a copy removal tool -- please test it on a non-critical folder first:
VBA Code:
Sub DelCopy()
    Dim f As FileDialog, s As String
    Set f = Application.FileDialog(msoFileDialogFolderPicker)
    With f
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then Set f = Nothing: Exit Sub
        s = .SelectedItems(1)
    End With
    Set f = Nothing
    s = """for /R """ & s & """ %A in (*.xl*) do del ""%~dpnA - Copy%~xA"""""
    Shell "cmd /c " & s
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,323
Messages
6,124,244
Members
449,149
Latest member
mwdbActuary

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