Copy files from one folder to another.

xBaelfire

New Member
Joined
Mar 13, 2021
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I have created a macro which counts if specific folder "CopyFrom" contains files begining with the name specified in column "B".
Formula takes number of rows to run macro "x" specified in "R1" and shows the count in column "S".

I am running into 2 problems here.

1. I can not make formula to start from the 4th row and first name to be taken from "B4"
2. I can make formula copy files with names: "B" & "-ACCDOC" & ".pdf" and "B" & "-ACCLINE" & ".pdf" to be copied into folder "CopyTo". I managed to copy 2 files if i entered date in B1 but after that formula was'nt going forward and searching for next files.

Please see code below.

Sub CountFiles()

Sheets("MoreThenOneFormulaValueChange").Activate

Dim i As Integer
Dim X As Integer
Dim Folder As String
Dim ExcelFN As String
Dim FileName As String
Dim FileName2 As String
Dim FileName3 As String
Dim NumFiles As Integer
Dim FSO
Dim filPath2 As String

X = Sheets("MoreThenOneFormulaValueChange").Range("R1").Value


For i = 2 To X


NumFiles = 0

Folder = "C:\Users\User\Desktop\CopyFROM\"
ExcelFN = Sheets("MoreThenOneFormulaValueChange").Range("B" & i).Value

FileName = Dir(Folder & ExcelFN & "*" & ".pdf")


While FileName <> ""
NumFiles = NumFiles + 1
FileName = Dir()
Wend



Sheets("MoreThenOneFormulaValueChange").Range("S" & i) = NumFiles







Next i

FileName2 = Dir(Folder & ExcelFN & "-ACCDOC" & ".pdf")
FileName3 = Dir(Folder & ExcelFN & "-ACCLINE" & ".pdf")
filPath2 = "C:\Users\USER\Desktop\CopyTo\"

Set FSO = CreateObject("Scripting.FileSystemObject")




Do While Len(FileName2) > 0

FSO.CopyFile (Folder & FileName2), filPath2
FileName2 = Dir

Loop

Do While Len(FileName3) > 0

FSO.CopyFile (Folder & FileName3), filPath2
FileName3 = Dir

Loop

End Sub

Thank you in advance for you help!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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