Kill *.xls

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
Can anyone tell me why this fails, the folder (range E1) is empty, but I get a "file not found" error on the second line
Code:
If Range("E1").Value & "\*.xls" <> "" Then
Kill Range("E1").Value & "\*.xls"
End If
Thank you
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
if E1 is "", then you're testing if "\*.xls"<>"", which evaluates true...

Provided the folder exists, you can fix it by removing & "\*.xls" from the If statement
 
Upvote 0
Do you have any .xls files in that folder when the code is ran? So long as you have at least one, and all of the files are in a state that they can be deleted from, you shouldn't get an error.
 
Upvote 0
Sometimes there are and sometimes not...it errored when there was no file in there...I thought it would bypass the Kill line if it found the folder empty?
 
Upvote 0
No, it needs to try to kill something. you can do some error handling to trap the "No .xls files in folder error".

Code:
Sub TEstIt()
 
    On Error GoTo killerror
    Kill Range("E1").Value & "\*.xls"
    On Error GoTo 0
 
    Exit Sub
 
killerror:
    Select Case Err.Number
    Case 53
        Resume Next
    Case Else
        MsgBox Err.Description, vbCritical, "Error Number: " & Err.Number
        End
    End Select
 
End Sub
 
Upvote 0
As Chris said, Range("E1").Value & "\*.xls" will never equal "" because it's a string which contains, at the very least, the characters "\*.xls". It isn't testing whether those files actually exist.

Try this:-
Code:
If [B][COLOR=red]Dir([/COLOR][/B]Range("E1").Value & "\*.xls"[COLOR=red][B])[/B][/COLOR] <> "" Then
  Kill Range("E1").Value & "\*.xls"
End If

Try to avoid using the On Error directive when you can test for the condition directly as this produces tidier code.
 
Upvote 0
You can test if there are any xls files in the folder using Dir.
Code:
If Dir(Range("E1").Value & "\*.xls") <> "" Then
       Kill Range("E1").Value & "\*.xls" 
End If
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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