Set Error handling INSIDE error-trap

M

Michelle

I am having a problem with my error trap.

I want to have my error-trap simplydisplay a message and close the data-file
that's being used. However, if the error is generated 'late-on' in my macro,
the data-file may already be closed, so to handle this, I am usingthe
following code:

'===========================
Exit Sub

ErrorTrap:
MsgBox "The Macro has encountered an unforeseen problem" & vbCr & "and
cannot continue", vbCritical, "Error " & Err
Application.Calculation = xlCalculationAutomatic

Err.Clear
On Error Resume Next ' in case the sheets are already protected or
data file is already closed

' and close the data file
DataFile.Close SaveChanges:=False
'===========================

but the 'On Error Resume Next ' line seems to be ignored because it
generates a standard VB error (with the Debug-box displayed). Is this
because I am in an error trap? is there a way around this?

thanks

M
 
J

Joel

Add a new variable to your macro call fileopen and initilize it to false

fileopen = false


Then when you open the file set it to true. When you close the file set it
false again. In you error code make the following change

ErrorTrap:
MsgBox "The Macro has encountered an unforeseen problem" & vbCr & "and
cannot continue", vbCritical, "Error " & Err
Application.Calculation = xlCalculationAutomatic

Err.Clear

' and close the data file
if fileopen then
DataFile.Close SaveChanges:=False
end if
 
T

Tim Williams

If after closing your datafile (in the "normal" flow) you 'Set DataFile =
Nothing' then you can test for that in the error handler

ie.

If Not DataFile Is Nothing then
DataFile.Close SaveChanges:=False
End If
 
M

Michelle

That's great, and thank you... but can I set error handling inside an error
trap?

M
 
M

Michelle

Thats an even better reply than the last one - thanks - but do you knowe if
I can change the way errors are handled in the error trap anyway - for
future reference.

M
 
J

Joel

You should be able to change the way errors are handling inside an error
haqndler. If is very normal to use On Error GoTo 0 inside an error handler
to return error handling to a normal mode.
 
P

Peter T

Once in the error handler code should be 100% safe so as not to generate an
error, other than an error designed to be trapped in the calling routine.

If that can't be guaranteed use Resume myLabel where myLabel is outside the
error handler (ie above it) and starts a new error handling routine.

Another approach might be to pass code to a dedicated procedure to handle
anything that might raise an error.

Following is highly contrived to demonstrate the above

' step through with F8
Sub aaa()
On Error GoTo errH
bbb
Exit Sub
errH:
MsgBox Err.Description, , Err.Number
End Sub

Sub bbb()
On Error GoTo errH
100 a = 1 / 0

110 a = 1 / 0

cleanup:
On Error Resume Next
' restore settings
200 a = 1 / 0
On Error GoTo errH
300 a = 1 / 0
Exit Sub
errH:
If Erl > 290 Then
Err.Raise 12345, _
Description:="problem in bbb line " & Erl & vbCr & _
Err.Description
ElseIf Erl = 100 Then
Err.Clear
Resume Next
ElseIf Erl = 110 Then
ccc
Resume cleanup
End If
End Sub

Sub ccc()
On Error Resume Next
a = 1 / 100 ' that's an error
End Sub

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top