Finance

Charts

Statistics

Macros

Search

Program with Runtime Errors In Excel VBA

The following procedure causes a runtime error. It divides the value in cell A1 by the value in cell A2, and outputs the result in cell A3:

Sub RuntimeError()
    Dim x As Integer, y As Integer, z As Integer
    ThisWorkbook.Worksheets("Sheet6").Activate
    x = Range("A1").Value
    y = Range("A2").Value
    z = x / y
    Range("A3").Value = z
    Range("A4").Value = "Done"
End Sub

Normal case:
If cells A1 and A2 contain the numbers 12 and 3, respectively, the result appears as expected .

Runtime error case:
If cells A1 and A2 contain 12 and 0, the program crashes with a runtime error. Neither the result nor the text « Done » is output. Instead, a dialog box appears with an error message.

You can choose to either End or Debug. Clicking Debug highlights the line causing the error . In this case, it is the line:

z = x / y

where the division by zero occurs. This helps you locate and fix the problem.

Type mismatch error:
If cells A1 and A2 contain 12 and the text « abc », the program crashes with a different error message .

Clicking Debug highlights the line:

y = Range(« A2 »).Value

. This happens because assigning the text « abc » to the integer variable y is incompatible.

After an error occurs:

  • Make sure to stop debugging if it hasn’t been stopped yet.
  • If debugging is still active, some options and information will not be available, and restarting the program won’t be possible.
  • You can stop debugging by selecting Run > Reset in the VBA editor menu or by clicking the corresponding reset button in the toolbar.
0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx