Visual Basic for Applications/Errors
This lesson introduces debugging and error handling.
Objectives and Skills
editObjectives and skills for debugging and error handling include:
- Using debugging tools to suspend and step through code
- Using debugging tools to examine code while it is running
- Incorporating error-handling code to respond to run-time errors
Readings
editMultimedia
editExamples
edit'This macro demonstrates error handling.
Option Explicit
Sub ErrorHandling()
Const Title = "Error Handling"
Dim Result As Single
On Error GoTo ErrorHandler
Result = 1 / 0
MsgBox "Result = " & Result, vbOKOnly + vbInformation, Title
Exit Sub
ErrorHandler:
Select Case MsgBox(Err.Description, vbAbortRetryIgnore + vbExclamation, Err.Source & " Error " & Err.Number)
Case vbAbort
End
Case vbRetry
Resume
Case vbIgnore
Resume Next
End Select
End Sub
Activities
editIn these activities you will create macros that use input validation and error handling to catch and respond to runtime errors. Your macros should include Option Explicit, Dim, InputBox, MsgBox, titles on the dialog boxes, an icon on the message box, and use appropriate data types for your variables.
- Age Calculations
- Create a macro that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds. Use an If/ElseIf/Else or Select Case statement to display their approximate age in the selected time frame.
- Use the IsNumeric() function to validate user input for the numeric entry.
- Use Else or Case Else to validate user input for the selected time frame.
- Add error handling to handle any runtime errors that could occur in the macro.
- Temperature Conversion
- Create a macro that asks the user if they would like to convert Fahrenheit to Celsius or Celsius to Fahrenheit. Use an If/ElseIf/Else or Select Case statement to determine their selection and then gather the appropriate input and calculate and display the converted temperature.
- Use Else or Case Else to validate user input for the selected conversion type.
- Use the IsNumeric() function to validate user input for the numeric entry.
- Add error handling to handle any runtime errors that could occur in the macro.
- Area Calculations
- Create a macro that asks the user what shape they would like to calculate the area for. Use an If/ElseIf/Else or Select Case statement to determine their selection and then gather the appropriate input and calculate and display the area of the shape.
- Use Else or Case Else to validate user input for the selected shape type.
- Use the IsNumeric() function to validate user input for any numeric entries.
- Add error handling to handle any runtime errors that could occur in the macro.
- Average Calculation
- Create a macro that asks the user to enter grade scores. Start by asking the user how many scores they would like to enter. Then use a loop to request each score and add it to a total. Finally, calculate and display the average for the entered scores.
- Use the IsNumeric() function to validate user input for each numeric entry.
- Add error handling to handle any runtime errors that could occur during the calculations.