View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Alex@JPCS Alex@JPCS is offline
external usenet poster
 
Posts: 33
Default Use of "On Error" within a loop

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

"Alastair" wrote in message
om...
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