Moving files via VBA - error handling if none to move?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
301
Office Version
  1. 365
Platform
  1. Windows
I have this
VBA Code:
Sub MoveFiles()

Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
   
' specify source and destination folders
sourcefile = "W:\COYA\interfaces\PAMLoader\failed\*"
DestFile = "W:\COYA\interfaces\PAMLoader\input\"
If sourcefile Is Nothing Then ' errors here with "Object Required"
    MsgBox "No files to process!"
    Exit Sub
End If

' move all files from source folder to destination folder
FSO.MoveFile Source:=sourcefile, Destination:=DestFile

End Sub

If there are no files to move, I'd like to exit gracefully. I added in the bit above but it doesn't like it.

What am I doing wrong?

Thanks in advance as always :)
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
It seems like this should work...
VBA Code:
If FSO.FileExists(sourcefile) Then
HTH. Dave
 
Upvote 1
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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