Error Handling Methods in Excel VBA
You can download the workbook used in this video from the link below:
https://www.nomorelinesla.com/wp-content/uploads/2022/04/Excel-VBA-Error-Handling-Examples.zip
Error-handling is one of the most useful aspects of VBA, not just in Excel but also in other applications such as Outlook and Access. Error-handling allows you to capture and potentially correct an issue with your VBA code.
Anyone who has programmed in VBA has seen the Microsoft Visual Basic window appear when an error occurs executing code. For example, if your code selects a workbook that does not exist within the workbook collection, an Error 9 "Subscript Out of Range" error will appear in a message box. Without error-handling, this message box fatally terminates the subroutines/functions currently running.
Error-handling allows you to set aside code that help identify or fix a potential issue that can halt a critical process from executing. This is especially important for reliable automated reporting.
The first error handling option is called "On Error Resume Next". Think of the "On Error" line of code as a stoplight that instructs Excel on what to do when the next fatal error occurs. It does not apply to any preceding code. "On Error Resume Next" tells Excel to continue running the next step of the subroutine/function. As expected, this has benefits and determents and thought go into selecting the correct method of error-handling.
The next error-handling option is called "On Error GoTo". You can give a section of your code a heading, i.e., "On Error GoTo NextSection". "NextSection" with a colon (i.e. NextSection:) would then need to be present within your code. The "On Error GoTo" and your section heading would then divert your code to the "NextSection:" part of your code. It is recommended that you put your "On Error GoTo" error handling at the end of your code to prevent your error handling from running accidentially. A way of doing this is to type "Exit Sub" preceding your error-handling heading.
Lastly the final error-handling option is called "On Error GoTo 0". The main different between "On Error GoTo 0" and "On Error GoTo 'Heading'" is "On Error GoTo 0" is the off-switch to any On Errors currently enabled within the subroutine. Any code following "On Error GoTo 0" will throw a fatal error if an execution error occurs, nullifying any error-handling currently active.
Note: when the subroutine finishes, the error-handling deactivates.
-
3:38:31
PudgeTV
20 hours ago🔵 Mod Mondays Ep 30 | SilverFoxGamr - How to Get HUGE! | Elden Ring Pre-Show & DLC
64.8K3 -
1:05:10
Donald Trump Jr.
20 hours agoThe American Dream is on the Ballot, Interview with Sean Davis | TRIGGERED Ep.148
164K156 -
2:02:31
Revenge of the Cis
17 hours agoEpisode 1342: You Fell For It
139K32 -
58:12
Savanah Hernandez
15 hours agoPropaganda goes into overdrive ahead of Thursday’s Presidential debate
97.1K79 -
1:04:25
Kimberly Guilfoyle
18 hours agoBiden Leaves White House Ahead of Debate, Plus Anti-Semitic LA Violence, Live with Mike Davis & Larry Elder | Ep. 136
115K98 -
1:03:55
In The Litter Box w/ Jewels & Catturd
1 day agoProvocation? | In the Litter Box w/ Jewels & Catturd – Ep. 593 – 6/24/2024
130K61 -
27:37
Stephen Gardner
17 hours ago🔴Trump DEFENDED by NYC Cuomo against Letitia James and Alvin Bragg
93.4K42 -
3:04
One Bite Pizza Reviews
3 days agoBarstool Pizza Review - Little Pops NY Pizzeria (Naperville, IL)
94.5K78 -
1:44:41
The Quartering
20 hours agoTrump Surges In Polls, AOC Flops, Biden's New CREEPY Hire, Trumps VP Pick Is In? w/ Styxhexenhammer
111K99 -
13:41
My Self Reliance
2 days agoPruning Fruit Trees in my Organic Orchard on the Summer Solstice | Food Forest Tour
88.2K12