Codes for On Error Resume Next
¬_.... from last post
Full pseudo code . ( ** For a simplified demonstration I am only considering the .Derscription for the Err, but you could use the same way , (of temporarily storing then re assigning after the On Error Goto -1 ) , for all the properties. )
Code:
Sub PseudoOnErrorResumeNextGoToGet5ButComeBackDarling() ' https://excelfox.com/forum/showthread.php/2239-Resume-On-Error-GoTo-0-1-GoTo-Error-Handling-Statements-Runtime-VBA-Err-Handling-ORNeRe-GoRoT-N0Nula-1?p=19875&viewfull=1#post19875
10 On Error GoTo ResNxt
20 Dim TNominator As Long, RslTwat As Long, errline As String
30 ' Other Code
40 Let TNominator = 0
50 Let RslTwat = 10 / TNominator
55 MsgBox Err.Description ' This does not give blank, as it would after a Resume Next
56 MsgBox errline ' This effectively gives the line where the last error occured
60 ' other code
70 Let TNominator = 0
80 Let RslTwat = 10 / TNominator
90 ' 0ther code
100 Exit Sub
110 ResNxt: ' "Error handling Code section" is from here until the End
120 Dim Answer As Long ' You could build this option in if you wanted to
122 Let Answer = MsgBox(prompt:="Your code errored: " & Err.Description & vbCrLf & "Do you want to continue?", Buttons:=vbYesNo)
124 If Answer = vbNo Then Exit Sub 'End code if user does not want to continue after error
130 Let errline = Erl ' this must be done before On Error GoTo -1 , as that clears the recorded error line
132 Dim TempErrDesc As String: Let TempErrDesc = Err.Description ' '_-In the case of the error object, Err , we only need to do that temporarily until just after we do the On Error GoTo -1 , since thereafter we can re assign the Err.Description, since , perhaps strangely, the error object, Err , is not just read only, as is the Erl(), and as logically we might have similarly expected that the Err should be
140 On Error GoTo -1
141 ' Err.Clear ' I do not need to do this, as it is effectively done as part of On Error GoTo -1 Note: Err.Clear removes the infomation, if an is present, in the Err object. it has no effect on the actual error state
143 Let Err.Description = TempErrDesc ' '_-In the case of the error object, Err , we only need to do that temporarily until just after we do the On Error GoTo -1 , since thereafter we can re assign the Err.Description, since , perhaps strangely, the error object, Err , is not just read only, as is the Erl(), and as logically we might have similarly expected that the Err should be
145 MsgBox prompt:="We want to go back to just after the erroring line " & errline
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 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
Just for the sake of comparison, and to emphasise the , perhaps unexpected required differences, I include again the previous pseudo coding for the Resume Next. In the pseudo coding done for the On Error Resume Next, above, I have made orange the most important differences. Those two most important differences are, in words,
_(i) the Error object , Err , does not get cleared in On Error Resume Next as it does in Resume Next
_(ii) related to (i), we can re assign the properties of the Error object , Err ( In either the normal code state or the in the State of Exception, so for the demo coding can use the real Err after, getting properties from it, rather than assigning, and using, variables for the various Err properties )
Code:
Sub PseudoResumeNextGoToGet5ButComeBackDarling() ' https://excelfox.com/forum/showthread.php/2239-Resume-On-Error-GoTo-0-1-GoTo-Error-Handling-Statements-Runtime-VBA-Err-Handling-ORNeRe-GoRoT-N0Nula-1?p=10557&viewfull=1#post10557
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
55 MsgBox Err.Description ' This gives blank. On Erro GoTo -1 has cleared the Err object of infomation
60 ' other code
70 Let TNominator = 0
80 Let RslTwat = 10 / TNominator
90 ' 0ther code
100 Exit Sub
110 GetMilkLuv: ' "Error handling Code section" is from here until the End
120 Dim Answer As Long ' You could build this option in if you wanted to
122 Let Answer = MsgBox(prompt:="Your code errored: " & Err.Description & vbCrLf & "Do you want to continue?", Buttons:=vbYesNo)
124 If Answer = vbNo Then Exit Sub 'End code if user does not want to continue after error
130 Dim errline As Long: Let errline = Erl ' this must be done before On Error GoTo -1 , as that clears the recorded error line
140 On Error GoTo -1
141 ' Err.Clear ' I do not need to do this, as it is effectively done as part of On Error GoTo -1 Note: Err.Clear removes the infomation, if an is present, in the Err object. it has no efffect on the actual error state
145 MsgBox prompt:="We want to go back to just after the erroring line " & errline
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 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
Bookmarks