ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Use of "On Error" within a loop (https://www.excelbanter.com/excel-programming/279459-use-error-within-loop.html)

Alastair[_2_]

Use of "On Error" within a loop
 
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

Alex@JPCS

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





All times are GMT +1. The time now is 01:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com