I am not sure what’s going on there. Maybe to explain it we need to know exactly what
On Error Resume Next and
On Error GoTo 0 does
The short response to your last post, Hans, could be, I don’t know for sure….
Maybe:
On Error GoTo 0 didn’t clear the state, because there wasn’t a state to be cleared.
On Error GoTo 0 cleared the
Err register thing
_.__________________________________________________________________________-
I don’t know for 100% sure.
In more detail, Maybe this…..
On Error GoTo 0
Possibly
On Error GoTo 0 also clears the
Err register thing.
Possibly the
Err register thing gets filled with information when an error occurs, regardless of anything else.
I am fairly sure , that
On Error GoTo 0 disables the error handler ( disables the error trapping ).
( As of a few hours ago it seems to me that it also does it in the exception state).
On Error Resume Next
Possibly
On Error Resume Next prevents the exception state ever being. So in your last macro the error state was never there.
On Error Resume Next tells VBA not to go into the exception state, but it does not prevent the
Err register thing being filled.
These variation of your last macro let me change the error trap. This suggest to me that
On Error Resume Next prevented the error state ever being raised
The second macro is also telling me that the
Err register thing gets filled with new information again after a different error occurs.
Code: Select all
Sub TestTraps()
Dim x
On Error Resume Next
x = 1 / 0
Debug.Print "Error before On Error GoTo Bed: " & Err.Number & Err.Description
On Error GoTo Bed
Debug.Print "Error after On Error GoTo Bed: " & Err.Number & Err.Description
x = 1 / 0
Exit Sub
Bed:
Debug.Print "Error in Bed: " & Err.Number & Err.Description
End Sub
Sub TerrorTraps()
Dim x, arrSpt() As Variant
On Error Resume Next
x = 1 / 0
Debug.Print "Error after divide by 0 : " & Err.Number & Err.Description
arrSpt() = Split("1 2", " ", 2, vbBinaryCompare) ' This will error because Split function returns an array of string type elements
Debug.Print "Error after Type mismatch : " & Err.Number & Err.Description
On Error GoTo Bed
Debug.Print "Error after On Error GoTo Bed: " & Err.Number & Err.Description
x = 1 / 0
Application.Wait Time:=(Now + TimeValue("0:00:10")) ' This code line will never be done
Exit Sub
Bed:
Debug.Print "Error in Bed: " & Err.Number & Err.Description
End Sub
What I am saying, ( or rather suggesting because I don’t know for sure ), is, regardless of what error handling/ trapping we are doing, the
Err register thing gets filled with the relevant information every time an error occurs. We can clear that register by either
On Error GoTo 0 or by changing the error trap to a different one, ( which I can do if I used
On Error Resume Next because I don’t have an error state ever if I used that)
In this next macro, my last erroring code line chucks up the default VBA error handler, but it does give me the correct information, so possibly this further confirms that the
Err register thing is filled even in the exception state, if I assume that the default VBA error handling takes its info from that
Err register thing.
Code: Select all
Sub TerrorWonks()
Dim x, arrSpt() As Variant
On Error Resume Next
x = 1 / 0
Debug.Print "Error after divide by 0 : " & Err.Number & Err.Description
arrSpt() = Split("1 2", " ", 2, vbBinaryCompare)
Debug.Print "Error after Type mismatch : " & Err.Number & Err.Description
On Error GoTo Bed
Debug.Print "Error after On Error GoTo Bed: " & Err.Number & Err.Description
x = 1 / 0
Application.Wait Time:=(Now + TimeValue("0:00:10")) ' This code line will never be done
Exit Sub
Bed:
Debug.Print "Initial Error in Bed: " & Err.Number & Err.Description
Let arrSpt() = Split("1 2", " ", 2, vbBinaryCompare)
Debug.Print "Error after Type mismatch : " & Err.Number & Err.Description 'this wont be done, but the default error handling tells me that I have a type mismatch, so possibly that is getting its infomation from the Err register thing
End Sub
_._______________
You often hear it said that
On Error Resume Next “suppresses” the error. I don’t know for sure if that means the same as
it doesn’t let the error state occur, ( or it only lets it exist for a short split second or so, so that the
Err register thing gets filled , then it went back to normal state but did not clear the
Err register thing )
The short response to your last post, Hans, could be, I don’t know for sure….
Maybe:
On Error GoTo 0 didn’t clear the state, because there wasn’t a state to be cleared.
On Error GoTo 0 cleared the
Err register thing
Code: Select all
Sub ErrorState()
Dim x
On Error GoTo ResumNext
x = 1 / 0
ResumNext:
' I am in exception state, and Err will tell me all about what error occured if I ask it
End Sub
Sub NoErrorState()
Dim x
On Error Resume Next
x = 1 / 0
' I am not in exception state, but Err will tell me all about what error occured if I ask it
End Sub
I expect nobody knows for sure. Those that possibly ever did , long since forgot.
Ref
On Error WTF
On Error Resume Next
https://www.myonlinetraininghub.com/err ... ing-in-vba
https://excelfox.com/forum/showthread.p ... #post15209
http://www.eileenslounge.com/viewtopic. ... 49#p247149
https://www.myonlinetraininghub.com/exc ... ment-84892
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also