Moving bulk files in VBA with FSO

anichols

Board Regular
Joined
Mar 11, 2021
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a code that moves a batch of files from one folder to another. my issue is when I try to change one of the locations to a cell reference, I end up with run time error 5.
Any help with what is likely a simple oversight would be greatly appreciated!

VBA Code:
Sub Move_Aires()
Dim FSO As New FileSystemObject
Dim WhereTo As String
Set FSO = CreateObject("Scripting.FileSystemObject")
WhereTo = Sheets("ARIES_REG").Range("D4").Value
FSO.MoveFile "P:\Servicing\are*.are", WhereTo
'FSO.MoveFile "P:\Servicing\are*.are", "P:\Servicing - 3. Batch Files\31. Aires\2022\2022.09.01\"

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Does Sheets("ARIES_REG").Range("D4").Value contain a value? If so, does it contain a valid path?
 
Last edited:
Upvote 0
Does Sheets("ARIES_REG").Range("D4").Value contain a value? If so, does it contain a valid path?
Yes. the commented out line contains the value from that cell instead. When I run it that way it is successful.
 
Upvote 0
I would suggest that you amend your code so it checks that both the source folder and destination folder exist before moving your files.

By the way, it looks like you're using early binding. As such, there's no need for Set FSO = CreateObject("Scripting.FileSystemObject"), since you are already creating an instance of the FileSystemObject object with Dim FSO As New FileSystemObject.

So your code can be re-written as follows...

VBA Code:
Sub Move_Aires()

    Dim FSO As New FileSystemObject
    Dim WhereFrom As String
    Dim WhereTo As String
   
    WhereFrom = "P:\Servicing\"
    If Right(WhereFrom, 1) <> "\" Then
        WhereFrom = WhereFrom & "\"
    End If
   
    If Not FSO.FolderExists(WhereFrom) Then
        MsgBox "The folder does not exist: " & vbCrLf & vbCrLf & WhereFrom, vbExclamation
        Exit Sub
    End If
   
    WhereTo = Sheets("ARIES_REG").Range("D4").Value
   
    If Not FSO.FolderExists(WhereTo) Then
        MsgBox "The folder does not exist: " & vbCrLf & vbCrLf & WhereTo, vbExclamation
        Exit Sub
    End If
   
    FSO.MoveFile WhereFrom & "are*.are", WhereTo

End Sub

However, if the source folder does not contain any .are files, you'll get the following error...

VBA Code:
Run-time error '53':

File not found

It probably would be better to loop through each file in the source folder, and check whether the file extension is .are. If so, move the file to the destination folder.

Hope this helps!
 
Upvote 0
Solution
I would suggest that you amend your code so it checks that both the source folder and destination folder exist before moving your files.

By the way, it looks like you're using early binding. As such, there's no need for Set FSO = CreateObject("Scripting.FileSystemObject"), since you are already creating an instance of the FileSystemObject object with Dim FSO As New FileSystemObject.

So your code can be re-written as follows...

VBA Code:
Sub Move_Aires()

    Dim FSO As New FileSystemObject
    Dim WhereFrom As String
    Dim WhereTo As String
  
    WhereFrom = "P:\Servicing\"
    If Right(WhereFrom, 1) <> "\" Then
        WhereFrom = WhereFrom & "\"
    End If
  
    If Not FSO.FolderExists(WhereFrom) Then
        MsgBox "The folder does not exist: " & vbCrLf & vbCrLf & WhereFrom, vbExclamation
        Exit Sub
    End If
  
    WhereTo = Sheets("ARIES_REG").Range("D4").Value
  
    If Not FSO.FolderExists(WhereTo) Then
        MsgBox "The folder does not exist: " & vbCrLf & vbCrLf & WhereTo, vbExclamation
        Exit Sub
    End If
  
    FSO.MoveFile WhereFrom & "are*.are", WhereTo

End Sub

However, if the source folder does not contain any .are files, you'll get the following error...

VBA Code:
Run-time error '53':

File not found

It probably would be better to loop through each file in the source folder, and check whether the file extension is .are. If so, move the file to the destination folder.

Hope this helps!
Awesome, thank you!
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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