title: vba if file exists then delete
body: <p>I am trying to delete a text file where it contains part of the previous dates month in the file name title and the current year.</p>
  
  <p>so for instance if today is November this would be month number 11 and last month would be month number 10. </p>
  
  <p>so I want to delete the file where it contains month number 10 because this is the previous month.</p>
  
  <p>for some reason my code causes excel to crash and not respond, can someone please show me how I can alter my code to get this working, thanks</p>
  
  <pre><code>Dim iMonth As Integer
  Dim iYear As Integer
  iMonth = Month(Date) - 1
  iYear = Year(Date)
  Dim DirFile As String
  DirFile = "P:\Log (" &amp; iMonth &amp; "-" &amp; iYear &amp; ")*.txt"
  Do While DirFile &lt;&gt; ""
  Count = Count + 1
  Loop
  If Count &gt; 0 Then
  SetAttr DirFile, vbNormal
  'Then delete the file
  Kill DirFile
  End If
  </code></pre>