Excel VBA Excel VBA Error Handling & Debugging 1 — Questions and Answers
Question 1: Which statement is used to enable error handling and redirect execution to a labeled section in Excel VBA?
- On Error GoTo (Correct answer)
- Try...Catch
- Error Handle
- Catch Error
Correct answer: On Error GoTo
'On Error GoTo' is the VBA statement that redirects execution to a specified error handler label when a runtime error occurs.
Question 2: What does 'On Error Resume Next' do in VBA?
- Stops execution immediately when an error occurs
- Jumps to the next module in the project
- Allows execution to continue with the statement following the error (Correct answer)
- Restarts the procedure from the beginning
Correct answer: Allows execution to continue with the statement following the error
'On Error Resume Next' instructs VBA to continue executing with the statement immediately following the one that caused the error.
Question 3: Which built-in VBA object is used to retrieve information about the most recently occurring error?
- ErrInfo
- Error
- Err (Correct answer)
- LastError
Correct answer: Err
The 'Err' object contains properties like Number, Description, and Source that provide details about the last error that occurred.
Question 4: What is the purpose of the Err.Clear method in VBA?
- Deletes all error history from the workbook
- Resets the Err object's properties to their default zero/empty values (Correct answer)
- Clears the VBA error log file
- Removes error-handling code from the procedure
Correct answer: Resets the Err object's properties to their default zero/empty values
Err.Clear resets the Err object's Number, Description, and Source properties to their default values (0 and empty strings).
Question 5: In VBA, what happens if a runtime error occurs inside an active error handler?
- VBA automatically retries the failing statement
- The error is silently ignored
- The current error handler is disabled and the error propagates to the caller (Correct answer)
- VBA jumps to a nested error handler
Correct answer: The current error handler is disabled and the error propagates to the caller
If an error occurs within an error handler, VBA disables that handler and passes the error up to the calling procedure's error handler.
Question 6: Which keyword is used at the end of an error handler to return execution to the main code flow?
- Exit Handler
- Resume (Correct answer)
- Return
- End Error
Correct answer: Resume
The 'Resume' keyword directs execution back to the main code — either re-trying the error line, moving to the next line, or jumping to a specified label.
Question 7: What does 'Resume Next' do when placed inside a VBA error handler?
- Re-runs the entire procedure from the start
- Jumps to the next procedure in the module
- Continues execution with the statement after the one that caused the error (Correct answer)
- Exits the current subroutine immediately
Correct answer: Continues execution with the statement after the one that caused the error
'Resume Next' directs VBA to continue execution with the statement immediately following the line that caused the error, effectively skipping it.
Which statement is used to enable error handling and redirect execution to a labeled section in Excel VBA?