Excel VBA – Copy or Move files from one folder to another

nmkhan3010

New Member
Joined
Feb 1, 2020
Messages
22
Office Version
  1. 2016
Platform
  1. Windows
Am having two folders “A” & “B” in A folder having 1000 files (word & rtf files) I need to copy only 10 files form the 1000 files (A folder) to B (folder). Is there any VBA code in excel for folder searching and copying.

10 files list is given excel column “B”

In column, C updated as “MOVED” if document found in A folder and moved to B folder

In column, C updated as “Does Not Exists” if document not found in A folder and gives an message.


Please modified the below code and it should be ask for source path and move to specific folder as not by default like below one’s.


Please do the needful and thanks in advance


Below code copied from other online sources only.....





Option Explicit



Sub CopyFiles()

Dim iRow As Integer ' ROW COUNTER.

Dim sSourcePath As String

Dim sDestinationPath As String

Dim sFileType As String



Dim bContinue As Boolean



bContinue = True

iRow = 2



' THE SOURCE AND DESTINATION FOLDER WITH PATH.

sSourcePath = "C:\Users\nkhaja\Desktop\PRODUCTION\2020\FEBRUARY\24-02-2020\14264660"

sDestinationPath = "C:\Users\nkhaja\Desktop\PRODUCTION\2020\FEBRUARY\24-02-2020\16580453"



sFileType = ".docx"

sFileType = ".rtf"

' TRY WITH OTHER FILE TYPES LIKE ".pdf".



' LOOP THROUGH COLUMN "B" TO PICK THE FILES.

While bContinue



If Len(Range("B" & CStr(iRow)).Value) = 0 Then ' DO NOTHING IF THE COLUMN IS BLANK.

MsgBox "Process executed" ' DONE.

bContinue = False

Else

' CHECK IF FILES EXISTS.



If Len(Dir(sSourcePath & Range("B" & CStr(iRow)).Value & sFileType)) = 0 Then

Range("C" & CStr(iRow)).Value = "Does Not Exists"

Range("C" & CStr(iRow)).Font.Bold = True

Else

Range("C" & CStr(iRow)).Value = "MOVED"

Range("C" & CStr(iRow)).Font.Bold = False



If Trim(sDestinationPath) <> "" Then

Dim objFSO

Set objFSO = CreateObject("scripting.filesystemobject")



' CHECK IF DESTINATION FOLDER EXISTS.

If objFSO.FolderExists(sDestinationPath) = False Then

MsgBox sDestinationPath & " Does Not Exists"

Exit Sub

End If



'*****

' HERE I HAVE INCLUDED TWO DIFFERENT METHODS.

' I HAVE COMMENTED THE SECOND METHOD. TO THE SEE THE RESULT OF THE

' SECOND METHOD, UNCOMMENT IT AND COMMENT THE FIRST METHOD.



' METHOD 1) - USING "CopyFile" METHOD TO COPY THE FILES.

objFSO.CopyFile Source:=sSourcePath & Range("B" & CStr(iRow)).Value & _

sFileType, Destination:=sDestinationPath



' METHOD 2) - USING "MoveFile" METHOD TO PERMANENTLY MOVE THE FILES.

'objFSO.MoveFile Source:=sSourcePath & Range("B" & CStr(iRow)).Value & _

sFileType, Destination:=sDestinationPath

'*****

End If

End If

End If



iRow = iRow + 1 ' INCREMENT ROW COUNTER.

Wend

End Sub








 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

davesexcel

Well-known Member
Joined
Feb 26, 2006
Messages
1,025
Search the forum for folder picker, here's one.

 

nmkhan3010

New Member
Joined
Feb 1, 2020
Messages
22
Office Version
  1. 2016
Platform
  1. Windows
Dialogue box to be pop up for selecting source folder and copied files also should pop up where i have to save the copied files....

Soucre Path : will be given at pop up window
Destination Path : will be given at pop up window

In column B There will be a file names
In column C Updated Status "Moved" Else "Does not Exists"

Please check and do the needul.....
 

Watch MrExcel Video

Forum statistics

Threads
1,112,993
Messages
5,543,177
Members
410,583
Latest member
jgalin
Top