Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KR KR is offline
external usenet poster
 
Posts: 121
Default Newbie Q: Error handling procedures in a loop

I've done a decent amount of low-level VBA coding, but I haven't done
anything at all with error handling (yet).

I have a loop that trys to open 5 different files from a target directory.
If the file is found, it will open the file and parse the data into arrays.

However, if the file is not found, I anticipate an error occuring when it
reaches:
Open MyLongFileName(n) For Input As #1
If it errors out, I want it to skip forward past the code that deals with
the file contents, and just start the next loop and try the next file.

My non-trained brain automatically assumes I have to set a boolean on
whether or not the file was found, using the error handler to set the value
completely separately ... but...

is there a way to set up the loop so that it all flows in-line? Basically to
just skip a bunch of code if an error occurs, within the loop. For example:

For EachFile = 1 to 5
On Error Go To NoFileFound 'for error handling
Open MyLongFileName(EachFile) For Input As #1
'do my stuff'
Close #1
NoFileFound: 'error handler
just continues to the next file loop
Next

Thanks for any advice/suggestions,
Keith
--
The enclosed questions or comments are entirely mine and don't represent the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Newbie Q: Error handling procedures in a loop

Error handling should realy only be used to handle unaticipated events, such
as the lack of a network connection, or something that will only happen once
ina blue moon. If you can reasonaly expect something to happen then you shoud
write your code to anticipate that event. (Error handling actually has a lot
of overhead that will slow down your code). My recomendation for your
procedure would be to use the Dir function just ahead of the open.

if Dir(FileName & Path) = Filename then
'open the file and read from it... with your code
end if

HTH

"KR" wrote:

I've done a decent amount of low-level VBA coding, but I haven't done
anything at all with error handling (yet).

I have a loop that trys to open 5 different files from a target directory.
If the file is found, it will open the file and parse the data into arrays.

However, if the file is not found, I anticipate an error occuring when it
reaches:
Open MyLongFileName(n) For Input As #1
If it errors out, I want it to skip forward past the code that deals with
the file contents, and just start the next loop and try the next file.

My non-trained brain automatically assumes I have to set a boolean on
whether or not the file was found, using the error handler to set the value
completely separately ... but...

is there a way to set up the loop so that it all flows in-line? Basically to
just skip a bunch of code if an error occurs, within the loop. For example:

For EachFile = 1 to 5
On Error Go To NoFileFound 'for error handling
Open MyLongFileName(EachFile) For Input As #1
'do my stuff'
Close #1
NoFileFound: 'error handler
just continues to the next file loop
Next

Thanks for any advice/suggestions,
Keith
--
The enclosed questions or comments are entirely mine and don't represent the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.



  #3   Report Post  
Posted to microsoft.public.excel.programming
KR KR is offline
external usenet poster
 
Posts: 121
Default Newbie Q: Error handling procedures in a loop

Excellent, this is a much preferable solution (and bonus- easy enough for me
to understand :)

"Jim Thomlinson" wrote in message
...
Error handling should realy only be used to handle unaticipated events,

such
as the lack of a network connection, or something that will only happen

once
ina blue moon. If you can reasonaly expect something to happen then you

shoud
write your code to anticipate that event. (Error handling actually has a

lot
of overhead that will slow down your code). My recomendation for your
procedure would be to use the Dir function just ahead of the open.

if Dir(FileName & Path) = Filename then
'open the file and read from it... with your code
end if

HTH



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Newbie: Help with Do Loop code Carl Excel Discussion (Misc queries) 3 December 2nd 04 07:04 PM
VBA Newbie: Help with Do Loop code Carl[_5_] Excel Programming 3 December 2nd 04 07:04 PM
Handling Null Field in Macro Loop Ken Excel Programming 2 October 19th 04 10:17 PM
Loop macro for a Newbie mike Excel Programming 3 December 31st 03 06:35 PM
newbie: stop a FOR NEXT loop Mike H[_7_] Excel Programming 4 October 11th 03 04:47 PM


All times are GMT +1. The time now is 05:13 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"