Macro To Move Files to different Folder

AnnieLox

New Member
Joined
Sep 18, 2017
Messages
22
Hello! First time for me to post!! I'm a self taught Macro maker so have patience please.
I am trying to create a macro to move files from one folder to another. I've tried reading and working off other macros I found on here but cant get it to work.

I have an excel sheet with a column of Vendors.

The folder I have will say something like "Bulk 5227834" and there will be up to 70 files with different numbers. I need all of the bulk to move from that folder to the vendor folder. additionally it can either be a PDF or an excel.

I have created a dummy folder to try and do this.

this is the code I have. (It doesn't work) but even if it did, I need the macro to look through the list of vendors, theres too many to write a code line for each.

Code:
Dim d As String, ext, x

    Dim FSO As Object
    Dim FromDir As String
    Dim ToDir As String
    Dim FExtension  As String
    Dim FNames As String
    Dim Files As String
    Dim LR As Long
  
    LR = Sheets("Macro").Range("A" & Rows.Count).End(xlUp).Row
    For RW = 2 To LR

    
    
Dim srcPath As String, destPath As String, srcFile As String
srcPath = "H:\Annie\delete\"
destPath = "H:\Annie\delete2\"
ext = Array("19", "April")
For Each x In ext
    d = Dir(srcPath & x)
        Do While d <> ""
            srcFile = srcPath & d
            FSO.MoveFile , destPath & d
            d = Dir
        Loop
Next
End Sub




any help will be greatly appreciated!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi & welcome to the board
Try this for your test folder
Code:
Sub MoveFiles()

    Dim d As String, ext As String, x As Variant
    Dim srcPath As String, destPath As String, srcFile As String

    Dim FSO As Object
    Dim LR As Long
    Dim Rw As Long
  
    Set FSO = CreateObject("scripting.filesystemobject")
    
'    LR = Sheets("Macro").Range("A" & Rows.Count).End(xlUp).Row
'    For Rw = 2 To LR
        srcPath = "H:\Annie\delete\"
        destPath = "H:\Annie\delete2\"
        ext = Array("*.xls", "*.pdf")
        For Each x In ext
            d = Dir(srcPath & x)
                Do While d <> ""
                    srcFile = srcPath & d
                    FSO.MoveFile srcPath & d, destPath & d
                    d = Dir
                Loop
        Next x
'    Next Rw
End Sub
If it works we can then look at looping through the folders
 
Upvote 0
I'm getting the Compile Error:
For Each may only iterate over a collection object or array

and it goes to this line of the code:

For Each x In ext
 
Upvote 0
Apologies, changed the Dim setting after I tested.
Code:
Dim d As String, ext As [COLOR=#ff0000]Variant[/COLOR], x As Variant
Make the change in red
 
Upvote 0
Yes!! it worked!:)

Ok
Where is the list of folders, you want to move files from, & where is the list of folders you want to move the files to?
Also do those lists show the complete folder path?
If not what is the base path?
 
Upvote 0
Thank you so much for all of your help!!

There's a large list (337), and it will need edits when we get new vendors.
my excel sheet is saved as
H:\Annie\Move Vendor Files

The folder where the files are is:T:\All Vendors Invoices\ALL VENDORS

and the folder where the files get moved to every day is:
T:\All Vendors Invoices\ (******insert vendor name here*******)


and the excel sheet is laid out as such


Path Dir Name
T:\All Vendors Invoices\23 FREIGHT 5B FREIGHT T:\All Vendors Invoices\ 23 FREIGHT 5B FREIGHT

 
Upvote 0
How can we tell which files belong to which vendors?
 
Upvote 0
the files are saved with the vendor in the name of the file. Additionally, some files they put a CK afterwards and those should not be moved- if that's possible.

Here's a sample of some from today:

2572441 Linden
2572453 Linden
2572352 Calumet
2572485 Bulk Express
2572509 First Choice
2572504 First Choice
2572679 Calumet
2572784 Boasso
2572991 cma CK
 
Upvote 0
Give this a go
Code:
Sub MoveFiles()

    Dim d As String, ext As Variant, x As Variant
    Dim srcPath As String, destPath As String, srcFile As String

    Dim FSO As Object
    Dim LR As Long
    Dim Rw As Long
  
    Set FSO = CreateObject("scripting.filesystemobject")
    
    With Sheets("Macro")
        For Rw = 2 To .Range("A" & Rows.Count).End(xlUp).Row
            srcPath = "H:\Annie\delete\"
            destPath = "H:\Annie\delete2\" & .Range("A" & Rw).Value & "\"
            ext = Array("*.xls", "*.pdf")
            For Each x In ext
                d = Dir(srcPath & x)
                    Do While d <> ""
                        If d Like "*" & .Range("A" & Rw).Value & "*" _
                            And Not d Like "*CK*" Then
                                srcFile = srcPath & d
                                FSO.MoveFile srcPath & d, destPath & d
                        End If
                        d = Dir
                    Loop
            Next x
        Next Rw
    End With
    
End Sub
This assumes that all the relevant directories exist, & that you have a list of vendors in col A of sheet "Macro" like

Excel 2013 32 bit
A
1Vendor Names
2Linden
3Calumet
4Bulk Express
5First Choice
Macro
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,628
Messages
6,120,618
Members
448,973
Latest member
ChristineC

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