Use of "On Error" within a loop

  • Thread starter Thread starter Alastair
  • Start date Start date
A

Alastair

Hi
I am using the following code (abbreviated) to extract data from daily
statistics files. For various reasons we may not get a file for every
day of the month. It works when all files exist, or when one stats
file is missing. If two files are missing then I get an "Error 1004
.... file could not be found" message when attempting to open the
second missing file.

For i = 1 To 31
On Error GoTo NoBook
Workbooks.Open Filename:="stats_day" & i & ".csv"
'
'process data from the opened workbook
'
NoBook:
Next i

I'm sure there will be a simple explanation, but I am stummped.

Thanks for your help
Alastair
 
Alastair,
Try:

For i = 1 To 31
On Error GoTo NoBook
Workbooks.Open Filename:="stats_day" & i & ".csv"
'
'process data from the opened workbook
'

A100:
Next i
.......
.......
.......
Exit Sub


NoBook:
Resume A100
End Sub


The "Resume" statement clears the error condition. Without it, the error
handler fails on the second error condition after handling the first.

Alex
 
Back
Top