tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,825
- Office Version
- 365
- 2019
- Platform
- Windows
According to this article by Chip Pearson, you should not use GoTo in error handling.
Error Handling In VBA
For example:
I do agree with him as I have come across an example where using GoTo did not achieve the desired result (although the example above does work with GoTo).
I could have added an error reset like this (and in examples that didn't work without it, it still didn't work with it):
What Chip Pearson did not say is what actually happens if you did use Goto instead of Resume.
Does anybody know?
Thanks
Error Handling In VBA
For example:
Code:
Dim MyArray(1 To 10) As VariantDim Counter As Integer
On Error GoTo Errhandler
For Counter = 1 To 20
MyArray(Counter) = Counter
Next Counter
Cont:
MsgBox "Finished"
Exit Sub
Errhandler:
MsgBox "Error"
GoTo Cont ' SHOULD USE RESUME Cont
I do agree with him as I have come across an example where using GoTo did not achieve the desired result (although the example above does work with GoTo).
I could have added an error reset like this (and in examples that didn't work without it, it still didn't work with it):
Code:
Dim MyArray(1 To 10) As Variant Dim Counter As Integer
On Error GoTo Errhandler
For Counter = 1 To 20
MyArray(Counter) = Counter
Next Counter
Cont:
MsgBox "Finished"
Exit Sub
Errhandler:
MsgBox "Error"
On Error Goto 0 ' Error reset
GoTo Cont ' SHOULD USE RESUME Cont
What Chip Pearson did not say is what actually happens if you did use Goto instead of Resume.
Does anybody know?
Thanks