On error resume next

DougRobertson

Active Member
Joined
Sep 22, 2009
Messages
334
Office Version
  1. 365
Platform
  1. Windows
Hi,

In my macro, there is a segment where it will need to Kill (delete) a file if it exists. However if it does NOT exist, I just want it to skip that command.

It works fine when there is a file to delete, but when there isn't, it stops with an error message "Can't find the file name" or some such thing. I tried putting the ON ERROR RESUME NEXT code one line above it, which works on other applications, but not this one.

Is there another way or command to have the macro disregard the error message and just keep going?

Thanks,

~ Doug
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Code:
if len(dir(sfile)) then kill sfile
 
Upvote 0
Doug

How about checking if the file exists before you try and kill it?
Code:
strFileName = "C:\FileToDelete.xls"
 
If Len(Dir(strFilename))>0 Then
 
      Kill strFileName
 
' optional Else
Else 
 
      MsgBox strFileName & " not found."
End If
 
Upvote 0
. . . works like a charm!

Thanks gentlemen.

Just one question though: What does the "Len" stand for? I'm assuming the Dir is Directory.

~ Doug
 
Upvote 0
Take a look in Help and post back if it's not clear.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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