• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Testing OnError - really basic question

PipBoy808

Member
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?
 
Hi PipBoy. You didn't tell the code what to print. ;)

Move the command down to the end, after you've done the calculation.
Code:
intResult = intValue1 / intValue2
Debug.Print intResult
 
Back
Top