We help IT Professionals succeed at work.

kill files

Hi
I use this code extract to delete all .xlsx in a folder but when there are no xlsx files the
code stops running.
What do I need to do to keep the code running if there are no .xlsx files

Dim ThisWorkbook As String
ThisWorkbook = ActiveWorkbook.Path
Kill ThisWorkbook & "\SCENARIOS PRICE FILES\*.XLSX"
Dim xrow As Long
xrow = 6
'create variables for the creation of the scenario worksheet and scenario workbook
Dim numbersheets As Integer

Thanks
David
Comment
Watch Question

GrahamSkanRetired
SILVER EXPERT
Top Expert 2012

Commented:
You can test for the presence of target files like this
Dim ThisWorkbook As String
ThisWorkbook = ActiveWorkbook.Path
If Dir(ThisWorkbook & "\SCENARIOS PRICE FILES\*.XLSX") <> "" then
    Kill ThisWorkbook & "\SCENARIOS PRICE FILES\*.XLSX"
end if
Dim xrow As Long
xrow 

Open in new window

= 6
Hi

try

    Dim Fname               As String
    Dim ThisWorkbookPath    As String
    
    ThisWorkbookPath = ActiveWorkbook.Path
    
    Fname = Dir(ThisWorkbookPath & "\SCENARIOS PRICE FILES\*.XLSX")
    
    If Len(Fname) Then
        Kill ThisWorkbookPath & "\SCENARIOS PRICE FILES\*.XLSX"
    End If

Open in new window


Kris
@ GrahamSkan

Didn't see your post :(
GrahamSkanRetired
SILVER EXPERT
Top Expert 2012

Commented:
Don't worry, neither did the questioner.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.