Hey guys. I've a real newbie question. I've sourced the following code from Microsoft's website in explaining the use of 'On Error' in VBA:
Sub TestErrorHandler()
'define variables
Dim intValue1 As Integer
Dim intvalue2 As Integer
'define error handler
On Error GoTo HandleError
' declare variable to store result
Dim intResult As Integer
' calculate result by dividing first value by second value
intResult = intValue1 / intvalue2
Exit Sub
HandleError:
MsgBox "I'm sorry, Dave. I can't let you do that: " & Err.Description
Exit Sub
End Sub
The above returns the appropriate message box. What I'd like to do is give intValue1 and intValue 2 values so as to write a procedure that does not return an error. I tried adding the following (orange text):
On Error GoTo HandleError
intValue1 = 10
intValue2 = 5
Debug. Print
' declare variable to store result
Dim intResult As Integer
' calculate result by dividing first value by second value
intResult = intValue1 / intvalue2
Exit Sub
I had hoped that this would return the value '2' in the immediate window, but instead I get nothing. I know I've made a really simple mistake somewhere, but where?
Sub TestErrorHandler()
'define variables
Dim intValue1 As Integer
Dim intvalue2 As Integer
'define error handler
On Error GoTo HandleError
' declare variable to store result
Dim intResult As Integer
' calculate result by dividing first value by second value
intResult = intValue1 / intvalue2
Exit Sub
HandleError:
MsgBox "I'm sorry, Dave. I can't let you do that: " & Err.Description
Exit Sub
End Sub
The above returns the appropriate message box. What I'd like to do is give intValue1 and intValue 2 values so as to write a procedure that does not return an error. I tried adding the following (orange text):
On Error GoTo HandleError
intValue1 = 10
intValue2 = 5
Debug. Print
' declare variable to store result
Dim intResult As Integer
' calculate result by dividing first value by second value
intResult = intValue1 / intvalue2
Exit Sub
I had hoped that this would return the value '2' in the immediate window, but instead I get nothing. I know I've made a really simple mistake somewhere, but where?