0
Public Function modCreditmove()
Dim fso, CB As String
Set fso = CreateObject("Scripting.FileSystemObject")
CB = ("H:\Credit*.xls")
If CB > 0 Then
fso.MoveFile ("H:\Credit*.xls"), "H:\Credit_Archive\"
End If

End Function

This is the code I am using to Move files for archive , I had the code working when there were files to move and then it threw an error code when there wasn't any files to move So I tried to add an if statement so there wasn't any error msgs popping up and I got the Run-time error '13', Type mismatch can someone help assist so i can do this the correct way. Thanks

2 Answers 2

1

CB is of type String. So when you do CB > 0, it tries to use it as a Integer.

If you want to check if there are any files in pattern, you can use Dir. If the result is an empty string, then nothing was found, so you want to move files when dir returns something other than empty.

Dim fso
Dim filePattern As String

filePattern = "H:\Credit*.xls"
If Dir(filePattern) <> "" Then
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.MoveFile filePattern, "H:\Credit_Archive\"
End If

Dir accepts a wildcard pattern, so I think this works exactly as you want it.

1
  • I was trying to make the CB represent the xls files I am moving. So how would I write this code if I wanted to make sure it worked only if there was at least 1 file my h drive to move ?
    – ELS31987
    Aug 21, 2013 at 21:26
0

Try this instead:

If (fso.FileExists("H:\Credit*.xls"))
    fso.MoveFile ("H:\Credit*.xls"), "H:\Credit_Archive\"
End If

http://msdn.microsoft.com/en-us/library/x23stk5t(v=vs.84).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.