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

nmkhan3010

New Member
Joined
Feb 1, 2020
Messages
23
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








 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Search the forum for folder picker, here's one.

 
Upvote 0
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.....
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,539
Members
449,038
Latest member
Guest1337

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