Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Error Handling then continue running

I've having some trouble understanding how to do this Error Handling thing.
Any assistance that's offered is greatly appreciated! My coding is at the
end.

The macro is opening a series of files, manipulating them, then resaving
them with a new name. The file names and manipulations are all listed in a
spreadsheet; the macro pulls one row of information, does its thing, then
moves on to the next row until it reaches its stop command.

Where I'm having trouble with the error handling is getting the macro to
continue on its way after an error (if I understand correctly what's going
on, it actually is continuing on its way, but then stops upon reaching a
second row with an error)

My goal is to have any errors put a message in a cell of the offending row,
rather than a pop-up message. There's actually a couple loops in the macro,
my thought is that I can put in several "on error goto" err_handler1,
err_handler2, etc., and have each err_handler populate a different message.
Depending on the step where the error occurs, I can then populate that cell
with a message that instructs the user what correction is necessary.

So here's the coding I was using on the first loop. Thanks in advance for
any help!

Do Until Grow = NextRow

Windows("Personal.xls").Activate
Sheets("Info Delivery").Activate

If IsEmpty(Cells(Grow, 28)) = True Then GFolder = Cells(Grow, 27)
Else GFolder = Cells(Grow, 27) & "\" & Cells(Grow, 28)
ChDir SourcePath & GFolder

If Cells(Grow, 41) = "Y" Then
Application.EnableEvents = False

On Error GoTo Err_Hdler1
Workbooks.Open Filename:=SourcePath & GFolder & "\" &
Cells(Grow, 33)
Application.EnableEvents = True

End If

Err_Hdler1:
Select Case Err.Number
Case Is = 1004
Range("F" & SummaryRow) = "File Not Found"
Case Else
Range("F" & SummaryRow) = "Unknown Error"
End Select

SummaryRow = SummaryRow + 1
Grow = Grow + 1
Loop


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Error Handling then continue running


Put error handling outside of the rest of the code.
To user error handling to write to the worksheet, structure it like this...
'(Note the "Exit Sub" and the "Resume Next")
'--
Sub GoAround()
On Error GoTo Err_Hdler1 '<<< At the top
'Declare variables here.
Sheets("Info Delivery").Activate
'Windows("Personal.xls").Activate '<<< What is this?

Do Until Grow = NextRow
If IsEmpty(Cells(Grow, 28)) = True Then
GFolder = Cells(Grow, 27)
Else
GFolder = Cells(Grow, 27) & "\" & Cells(Grow, 28)
End If
ChDir SourcePath & GFolder

If Cells(Grow, 41) = "Y" Then
Application.EnableEvents = False
Workbooks.Open Filename:=SourcePath & GFolder & "\" & Cells(Grow, 33)
Application.EnableEvents = True
End If
Loop
Exit Sub

Err_Hdler1:
Select Case Err.Number
Case Is = 1004
Range("F" & SummaryRow) = "File Not Found"
Case Else
Range("F" & SummaryRow) = "Unknown Error"
End Select
SummaryRow = SummaryRow + 1
Grow = Grow + 1
Resume Next
End Sub
--
Jim Cone
Portland, Oregon USA




"modo8"
wrote in message
I've having some trouble understanding how to do this Error Handling thing.
Any assistance that's offered is greatly appreciated!
My coding is at the end.

The macro is opening a series of files, manipulating them, then resaving
them with a new name. The file names and manipulations are all listed in a
spreadsheet; the macro pulls one row of information, does its thing, then
moves on to the next row until it reaches its stop command.

Where I'm having trouble with the error handling is getting the macro to
continue on its way after an error (if I understand correctly what's going
on, it actually is continuing on its way, but then stops upon reaching a
second row with an error)

My goal is to have any errors put a message in a cell of the offending row,
rather than a pop-up message. There's actually a couple loops in the macro,
my thought is that I can put in several "on error goto" err_handler1,
err_handler2, etc., and have each err_handler populate a different message.
Depending on the step where the error occurs, I can then populate that cell
with a message that instructs the user what correction is necessary.

So here's the coding I was using on the first loop. Thanks in advance for
any help!

Do Until Grow = NextRow
Windows("Personal.xls").Activate
Sheets("Info Delivery").Activate
If IsEmpty(Cells(Grow, 28)) = True Then GFolder = Cells(Grow, 27)
Else GFolder = Cells(Grow, 27) & "\" & Cells(Grow, 28)
ChDir SourcePath & GFolder
If Cells(Grow, 41) = "Y" Then
Application.EnableEvents = False
On Error GoTo Err_Hdler1
Workbooks.Open Filename:=SourcePath & GFolder & "\" &
Cells(Grow, 33)
Application.EnableEvents = True
End If
Err_Hdler1:
Select Case Err.Number
Case Is = 1004
Range("F" & SummaryRow) = "File Not Found"
Case Else
Range("F" & SummaryRow) = "Unknown Error"
End Select
SummaryRow = SummaryRow + 1
Grow = Grow + 1
Loop
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Error Handling then continue running

Error handling in VBA is far from optimal. The best way to deal with a
"looping attempt" is something along the lines of...

On Error Resume Next
Workbooks.Open....
if err.number < 0 then
report it
err.clear
end if

If you put an On Error Goto inside a loop, it will only "work" the
first time, and from then on the errors will not get trapped. I'm sure
there's a way around that, but the solution above should do what you
want.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Error Handling then continue running

Thank you both so much! I went with Maury's way and it worked well. I was
able to use the same language in each loop to handle different errors.

For anyone else with this problem, here's the coding I wound up using:

Do Until Grow = NextRow

On Error Resume Next

Windows("Personal.xls").Activate
Sheets("Info Delivery").Activate

If IsEmpty(Cells(Grow, 28)) = True Then GFolder = Cells(Grow, 27) Else
GFolder = Cells(Grow, 27) & "\" & Cells(Grow, 28)
ChDir SourcePath & GFolder

If Cells(Grow, 41) = "Y" Then
Application.EnableEvents = False
Workbooks.Open Filename:=SourcePath & GFolder & "\" & Cells(Grow, 33)

If Err.Number < 0 Then
If Err.Number = 1004 Then Range("F" & SummaryRow) = "File not
found" else Range("F" & SummaryRow) = "Unknown error"
Err.Clear
End If

Application.EnableEvents = True

End If

SummaryRow = SummaryRow + 1
Grow = Grow + 1
Loop
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
Continue running macro until last line of data. Steve Excel Programming 1 November 24th 08 07:56 AM
Error handling error # 1004 Run-time error [email protected] Excel Programming 3 May 20th 08 02:23 PM
How to continue macro if there is an error value msdrolf Excel Programming 1 November 2nd 06 07:23 PM
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
Error handling with a handling routine ben Excel Programming 0 March 15th 05 03:01 PM


All times are GMT +1. The time now is 11:18 AM.

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

About Us

"It's about Microsoft Excel"