shame about the ******
(caan of couse in VBA editor get rid of them by Find and Replace ( Find * Replace with space !!!!)
Codes for On Error Resume Next
Pseudo On Error Resume Next
The code has two errors . Effectively both are “ignored” – The code continues in un aroused normal modus just after the erroring lines, but we have in the Err object information about the last error.
Code:
Sub PseudoOnErrorResumeNextGoComeBackAJackQuickCrap()
10 On Error GoTo GetMilkLuv
20 Dim TNominator As Long, RslTwat As Long
30 ' Other Code
40 Let TNominator = 0
50 Let RslTwat = 10 / TNominator ' This will error because of an attempt to divide by zero
55 MsgBox Err.Number & " " & Err.Description ' This does give infomation despite that the On Erro GoTo -1 has cleared the Err object of infomation. We put the infomation back
60 ' other code
70 Dim Rng As Range
80 Let Rng.Value = "Anyfink" ' This line should error as we have not assigned any object to rng. ( We cannot therefore asssing a Value to a non existing range
85 MsgBox Err.Number & " " & Err.Description
90 ' 0ther code
100 Exit Sub
110 GetMilkLuv: ' "Error handling Code section" is from here until the End
120 Dim errLine As Long: Let errLine = Erl ' this must be done before On Error GoTo -1 , as that clears the recorded error line
130 Dim errNumber As Long, errDescription As String ' ' this must be done before On Error GoTo -1 , as that clears Err object of error information
132 Let errNumber = Err.Number: Let errDescription = Err.Description ' This would be a fairly typical use of the Err object to get infomation about the error from that held in the object. As typical in object orintated programming, these "thing" of the object are referred to as Properties. We are retrieving the Properties of Errror Number and Description and holding them in apprpriately declared ( Dim ed ) variables
140 On Error GoTo -1
141 MsgBox prompt:="We want to go back to just after the erroring line " & errLine & vbCrLf & "and continue in normal code run mode" & vbCrLf & "but we want the Err object to hold infomation about the last error" & vbCrLf & "In the real On Error Resume Next we can not display this message as" & vbCrLf & "the error handling code section is effectively internal and we cannot hook a call back code into it"
143 Let Err.Number = errNumber ' Like many object properties, here we may referrence them and assign them using VBA.
145 Let Err.Description = errDescription
150 Select Case errLine:
Case 10: GoTo 20
Case 20: GoTo 30
Case 30: GoTo 40
Case 40: GoTo 50
Case 50: GoTo 55
Case 55: GoTo 60
Case 60: GoTo 70
Case 70: GoTo 80
Case 80: GoTo 85
Case 85: GoTo 90
Case 90: GoTo 100
Case 100: GoTo 110
Case 110: GoTo 120
Case 120: GoTo 130
Case 130: GoTo 140
Case 140: GoTo 150
End Select
End Sub
'
VBA On Error Resume Next
Here the code using the actual VBA On Error Resume Next error handling statement is used to do the same as the previous code with the exception that we cannot have the MsgBox come up which was previously within the error handling code section: Effectively the actual error handling code section used in the previous code is what VBA internally does , ( with the exception that VBA does not give us any message, and we do not have a simple way to hook our code into it). But as seen we can get the information as previously in the main code relating to the type of error that occurred.
This code one is one of the most simplest considered so far. The Pseudo version was one of the most complicated. There is a lot of crap hidden behind this On Error Resume Next. It is generally not thought of as a good idea to do by most experts. The fact that a lot of illogical stuff is “hidden behind it” is probably another reason to avoid it if at all possible.
Code:
Sub VBAOnErrorResumeNext()
On Error Resume Next
Dim TNominator As Long, RslTwat As Long
' Other Code
Let TNominator = 0
Let RslTwat = 10 / TNominator ' This will error because of an attempt to divide by zero
MsgBox Err.Number & " " & Err.Description ' This does give infomation despite that the exception has been cleared.. wierd and not as one might have expected.
' other code
Dim Rng As Range
Let Rng.Value = "Anyfink" ' This line should error as we have not assigned any object to rng. ( We cannot therefore asssing a Value to a non existing range
MsgBox Err.Number & " " & Err.Description
' 0ther code
End Sub
Bookmarks