I have a Sub in VBA (on a Mac) to delete a file, and it works correctly when the file exists. However, I get "Run-time error '53' File not found" when the file doesn't exist, even though the code is checking if it exists.
I tried a couple of different subs including from http://word.mvps.org/faqs/macrosvba/DeleteFiles.htm and Deleting a file in VBA
Sub DeleteFile()
Dim KillFile As String
KillFile = "/Users/me/Downloads/myfile.csv"
'Check that file exists
If Len(Dir$(KillFile)) > 0 Then
'First remove readonly attribute, if set
SetAttr KillFile, vbNormal
'Then delete the file
Kill KillFile
Else
End If
End Sub
Why am I getting this error when it should already be handled by the If statement?
Thanks.
EDIT: I want to delete/search for only this specific file, can I do that with Dir and MacID? I don't need to comb the directory for all CSV or TEXT files.