VBA: Moving files to different folders

Ziri

New Member
Joined
Mar 19, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello VBA Experts,

I have been trying to write a code for files (listed in column A) that I currently located in folder listed in column P to folders listed in column O. I have tried several approaches and have read many forums but without any results. Find below the last coda i tried but it is giving me "Invalide procedure call or argument" error. Could anyone push me the right direction please?

VBA Code:
Sub Move_Files_From_One_Folder_To_Another_Folder()
Dim FSO As Object
Dim FromDir As String
Dim ToDir As String
Dim FExtension As String
Dim FNames As String
  
  Sheets("Data").Activate

Dim A As String
Dim B As String
Dim C As String
Dim i As Long

    For i = 1 To 100000
    A = Range("A" & i)
B = Range("O" & i)
    C = Range("P" & i)
    Next
FromDir = C & A
ToDir = B & A
'We define here the types of files we wish to move. If you had .txt or .docx or .doc files you can use *.* as the FExtension
FExtension = "*.pdf"
'Now assign each file name with extension
FNames = Dir(FromDir & FExtension)
'Check whether there are any files in the folder so that you can exit if there are no files
If Len(FNames) = 0 Then

MsgBox ("No files in " & FromDir)
Exit Sub
End If
'Most methods return values but FSO returns an object. You cannot simply assign an object to a variable using an equals sign. In Excel VBA you need to use the Set statement
Set FSO = CreateObject("Scripting.FileSystemObject")
'Now we move the file from the source directory to the destination directory
FSO.MoveFile Source:=FromDir & FExtension, Destination:=ToDir
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Code:
For i = 1 To 100000
vFileNam = Range("A" & i)
vSrcDir = Range("O" & i)
vTargDir = Range("P" & i)

vSrcFile = vSrcDir & vFileNam
vTargFile = vTargDir & vFileNam

'We define here the types of files we wish to move. If you had .txt or .docx or .doc files you can use *.* as the FExtension
'FExtension = "*.pdf"

If InStr(vFileNam, ".pdf") > 0 Then
    Name vSrcFile As vTargFile
End If
Next
 
Upvote 0
Thank you for the code. I tried to changed it but it still gives mi the same error.

VBA Code:
Sub Move_Files_From_One_Folder_To_Another_Folder()
Dim FSO As Object
Dim FromDir As String
Dim ToDir As String
Dim FExtension As String
Dim FNames As String
    Sheets("Data").Activate
    Dim A As String
Dim B As String
Dim C As String
    Dim i As Long

For i = 1 To 100000
vFileNam = Range("A" & i)
vSrcDir = Range("O" & i)
vTargDir = Range("P" & i)
vSrcFile = vSrcDir & vFileNam
vTargFile = vTargDir & vFileNam
'We define here the types of files we wish to move. If you had .txt or .docx or .doc files you can use *.* as the FExtension
FExtension = "*.pdf"
If InStr(vFileNam, ".pdf") > 0 Then
Name vSrcFile As vTargFile
End If
Next
'Most methods return values but FSO returns an object. You cannot simply assign an object to a variable using an equals sign. In Excel VBA you need to use the Set statement
Set FSO = CreateObject("Scripting.FileSystemObject")
'Now we move the file from the source directory to the destination directory
FSO.MoveFile vSrcFile, vTargFile
End Sub
 
Upvote 0
Hello!
Can you post the sample data with XL2BB please?
 
Upvote 0
Hello,

here are some sample data. Thank you!

makro.xlsm
ABCD
1FileFile wt extDestination FolderSource Folder
2196585196585.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
3200282200282.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
4200480200480.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
5200488200488.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
6200490200490.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
7200536200536.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
8200543200543.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
9200544200544.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
10200545200545.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
11200546200546.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
12200547200547.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
13200564200564.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
14200567200567.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
15200578200578.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
16200587200587.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
17200588200588.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
18200591200591.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
19200593200593.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
20200597200597.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
21200607200607.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
22200609200609.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
23200610200610.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
24200611200611.pdfZ:\Ucetnictvi\Capexus\FAKTURY PŘIJATÉ\STAV NOUZE\Makro\Doklady zaplacené\C:\Users\tereza lhotska\Desktop\Makro\
List7
 
Upvote 0
So column B contains the files to be moved from the folder(s) within column D to the folder(s) within column C, am I right?
 
Upvote 0
So column B contains the files to be moved from the folder(s) within column D to the folder(s) within column C, am I right?

Yes, in column B there is a file to be move to folder in column D from column C.
 
Upvote 0
here are some sample
If it is the real structure of data, try this on its copy.
VBA Code:
Option Explicit

Sub MoveByList()
Dim fso, f, r: r = 2    'r - row w first file name  in A column

Set fso = CreateObject("Scripting.FileSystemObject")

Do While Not IsEmpty(Cells(r, 1))
    Set f = fso.GetFile((Cells(r, 4)) & "\" & Cells(r, 2))
    f.Move Cells(r, 3) & "\"
    r = r + 1
Loop
End Sub
 
Upvote 0
to folder in column D from column C.
Pardon me, don't understand in what column there is the source path, and in what destination is? My code for D as source and C as destination.
 
Upvote 0
If it is the real structure of data, try this on its copy.
VBA Code:
Option Explicit

Sub MoveByList()
Dim fso, f, r: r = 2    'r - row w first file name  in A column

Set fso = CreateObject("Scripting.FileSystemObject")

Do While Not IsEmpty(Cells(r, 1))
    Set f = fso.GetFile((Cells(r, 4)) & "\" & Cells(r, 2))
    f.Move Cells(r, 3) & "\"
    r = r + 1
Loop
End Sub

Thank you. The code works very well. The last think I would need to sort is if the file doesn't exist to skip it and continue with the next one.
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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