[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

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
0
DAVID131
Asked:
DAVID131
  • 2
  • 2
1 Solution
 
GrahamSkanCommented:
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
0
 
krishnakrkcCommented:
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
0
 
krishnakrkcCommented:
@ GrahamSkan

Didn't see your post :(
0
 
GrahamSkanCommented:
Don't worry, neither did the questioner.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now