View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Matt[_40_] Matt[_40_] is offline
external usenet poster
 
Posts: 19
Default Macro in Excel using an Array to Delete File in Directory if it Exists

Hello everyone:

I am trying to use an array in an Excel XP macro to delete a file(s)
if they exist. I have written some code and attached it but I am
getting a Compilte Error message when it hits the line
"Files(sDir).Select". I think that I need to change the word Files in
that line of code, but am not sure what to change it to, assuming I
have written that part correct. Any help would be greatly
appreciated. Thanks in advance.

Matt


Sub Delete_Dir_If_Exists()

Dim sbj As String
Dim sCustomer As String
Dim sLease As String
Dim sWellNo As String
Dim sSum As String
Dim sProc As String
Dim sVol As String
Dim sPres As String
Dim sBlen As String
Dim sDir As File

ActiveWorkbook.Sheets("SC Database").Activate
sCustomer = ActiveSheet.Range("Customer")
sField = ActiveSheet.Range("Field")
sWell = ActiveSheet.Range("Well")
sbj = sCustomer & " " & sField & " " & sWell & " "
sSum = sbj & "Onsite.doc"
sProc = sbj & "Pumping.doc"
sVol = sbj & "Volume.doc"
sPres = sbj & "Pressure.doc"
sBlen = sbj & "Calculations.doc"

' Finds out if the file already exists, and if it does deletes it.
sDirectory = Array(sSum, sProc, sVol, sPres, sBlen)
For Each sDir In sDirectory
Files(sDir).Select
If
CreateObject("scripting.fileSystemObject").FileExi sts(.Directory) Then
Kill MyPath & sDirectory
Else
End If
Next sDir
End Sub