On Error Goto Vba Example
Contents |
three flavors: compiler errors such as undeclared variables that prevent your code from compiling; user data entry error such as a user entering a negative value where only a positive number is acceptable; and run time errors, that occur when VBA cannot correctly execute excel vba try catch a program statement. We will concern ourselves here only with run time errors. Typical run time
On Error Goto Line
errors include attempting to access a non-existent worksheet or workbook, or attempting to divide by zero. The example code in this article will use
Vba Error Handling Best Practices
the division by zero error (Error 11) when we want to deliberately raise an error. Your application should make as many checks as possible during initialization to ensure that run time errors do not occur later. In Excel, this includes
Vba On Error Exit Sub
ensuring that required workbooks and worksheets are present and that required names are defined. The more checking you do before the real work of your application begins, the more stable your application will be. It is far better to detect potential error situations when your application starts up before data is change than to wait until later to encounter an error situation. If you have no error handling code and a run time error occurs, VBA will display its standard vba on error goto 0 run time error dialog box. While this may be acceptable, even desirable, in a development environment, it is not acceptable to the end user in a production environment. The goal of well designed error handling code is to anticipate potential errors, and correct them at run time or to terminate code execution in a controlled, graceful method. Your goal should be to prevent unhandled errors from arising. A note on terminology: Throughout this article, the term procedure should be taken to mean a Sub, Function, or Property procedure, and the term exit statement should be taken to mean Exit Sub, Exit Function, or Exit Property. The term end statement should be taken to mean End Sub , End Function, End Property, or just End. The On Error Statement The heart of error handling in VBA is the On Error statement. This statement instructs VBA what to do when an run time error is encountered. The On Error statement takes three forms. On Error Goto 0 On Error Resume Next On Error Goto
generally put more focus on the coding part and getting the desired result but during this process we forget an important thing i.e. Error handling. vba error handling in loop Error handling is an important part of every code and VBA On Error vba error number Statement is an easy way for handling unexpected exceptions in Excel Macros. A well written macro is one that includes vba iferror proper exception handling routines to catch and tackle every possible error. Error handling is important because in case of any unexpected exceptions your code doesn’t break. Even if any fatal unexpected error http://www.cpearson.com/excel/errorhandling.htm occurs in the code then also you should ensure that the code should terminate gracefully. Definition of VBA On Error Statement: On Error statement instructs VBA Compiler, what to do in case any runtime exception are thrown. Syntax of On Error Statement: Basically there are three types of On Error statement: On Error Goto 0 On Error Resume Next On Error Goto
execution at a specified line upon hitting an error. Situation: Both programs calculate the square root of numbers. Square Root 1 Add the following code lines to the 'Square Root 1' command button. 1. First, http://www.excel-easy.com/vba/examples/error-handling.html we declare two Range objects. We call the Range objects rng and cell. Dim rng http://www.firststeps.ru/vba/vbahelp/r.php?38 As Range, cell As Range 2. We initialize the Range object rng with the selected range. Set rng = Selection 3. We want to calculate the square root of each cell in a randomly selected range (this range can be of any size). In Excel VBA, you can use the For Each Next loop for this. Add the following on error code lines: For Each cell In rng Next cell Note: rng and cell are randomly chosen here, you can use any names. Remember to refer to these names in the rest of your code. 4. Add the following code line to the loop. On Error Resume Next 5. Next, we calculate the square root of a value. In Excel VBA, we can use the Sqr function for this. Add the following code line to on error goto the loop. cell.Value = Sqr(cell.Value) 6. Exit the Visual Basic Editor and test the program. Result: Conclusion: Excel VBA has ignored cells containing invalid values such as negative numbers and text. Without using the 'On Error Resume Next' statement you would get two errors. Be careful to only use the 'On Error Resume Next' statement when you are sure ignoring errors is OK. Square Root 2 Add the following code lines to the 'Square Root 2' command button. 1. The same program as Square Root 1 but replace 'On Error Resume Next' with: On Error GoTo InvalidValue: Note: InvalidValue is randomly chosen here, you can use any name. Remember to refer to this name in the rest of your code. 2. Outside the For Each Next loop, first add the following code line: Exit Sub Without this line, the rest of the code (error code) will be executed, even if there is no error! 3. Excel VBA continues execution at the line starting with 'InvalidValue:' upon hitting an error (don't forget the colon). Add the following code line: InvalidValue: 4. We keep our error code simple for now. We display a MsgBox with some text and the address of the cell where the error occurred. MsgBox "can't calculate square root at cell " & cell.Address 5. Add the following line
îøèáîê, íà÷àëî êîòîðîé îïðåäåëÿåòñÿ îáÿçàòåëüíûì àðãóìåíòîì ñòðîêà, çíà÷åíèåì êîòîðîãî ìîæåò áûòü ëþáàÿ ìåòêà ñòðîêè èëè íîìåð ñòðîêè. Åñëè âîçâðàùàåòñÿ îøèáêà âûïîëíåíèÿ, óïðàâëåíèå ïåðåäàåòñÿ íà óêàçàííóþ ñòðîêó è çàïóñêàåòñÿ îáðàáîò÷èê îøèáîê. Àðãóìåíò ñòðîêà äîëæåí îïðåäåëÿòü ñòðîêó â òîé æå ïðîöåäóðå, â êîòîðîé íàõîäèòñÿ èíñòðóêöèÿ On Error; â ïðîòèâíîì ñëó÷àå âîçíèêàåò îøèáêà êîìïèëÿöèè. On Error Resume Next - Óêàçûâàåò, ÷òî âîçíèêíîâåíèå îøèáêè âûïîëíåíèÿ ïðèâîäèò ê ïåðåäà÷å óïðàâëåíèÿ íà èíñòðóêöèþ, íåïîñðåäñòâåííî ñëåäóþùóþ çà èíñòðóêöèåé, ïðè âûïîëíåíèè êîòîðîé âîçíèêëà îøèáêà. Ðåêîìåíäóåòñÿ ïðè äîñòóïå ê îáúåêòàì èñïîëüçîâàòü ýòó ôîðìó èíñòðóêöèè, à íå On Error GoTo. On Error GoTo 0 - Îòêëþ÷àåò ëþáîé àêòèâèçèðîâàííûé îáðàáîò÷èê îøèáîê â òåêóùåé ïðîöåäóðå. Çàìå÷àíèÿ Åñëè íå âûïîëíåíà èíñòðóêöèÿ On Error, òî ëþáàÿ îøèáêà âûïîëíåíèÿ ÿâëÿåòñÿ ôàòàëüíîé; ýòî îçíà÷àåò, ÷òî âûâîäèòñÿ ñîîáùåíèå îá îøèáêå è âûïîëíåíèå ïðîãðàììû ïðåêðàùàåòñÿ. "Âêëþ÷åííûì" îáðàáîò÷èêîì îøèáîê íàçûâàþò ïîäïðîãðàììó, êîòîðàÿ óêàçàíà â èíñòðóêöèè On Error; "àêòèâíûì" îáðàáîò÷èêîì îøèáîê ÿâëÿåòñÿ âêëþ÷åííûé îáðàáîò÷èê îøèáîê, êîòîðûé îáðàáàòûâàåò òåêóùóþ îøèáêó. Åñëè îøèáêà âîçíèêàåò â ñàìîì îáðàáîò÷èêå îøèáîê (â ïðîìåæóòêå ìåæäó âîçíèêíîâåíèåì îøèáêè è âûïîëíåíèåì èíñòðóêöèè Resume, Exit Sub, Exit Function èëè Exit Property), òî îáðàáîò÷èê îøèáîê, îïðåäåëåííûé â òåêóùåé ïðîöåäóðå, íå ìîæåò îáðàáîòàòü îøèáêó. Óïðàâëåíèå â ýòîì ñëó÷àå âîçâðàùàåòñÿ â âûçûâàþùóþ ïðîöåäóðó; åñëè â âûçûâàþùåé ïðîöåäóðå âêëþ÷åí îáðàáîò÷èê îøèáîê, òî îáðàáîòêà îøèáêè ïåðåäàåòñÿ åìó. Åñëè ýòîò îáðàáîò÷èê îøèáîê ÿâëÿåòñÿ â äàííûé ìîìåíò àêòèâíûì, ò.å. óæå îáðàáàòûâàåò îøèáêó, òî óïðàâëåíèå ñíîâà ïåðåäàåòñÿ íàçàä â âûçûâàþùóþ ïðîöåäóðó è ò.ä. äî òåõ ïîð, ïîêà íå áóäåò íàéäåí âêëþ÷åííûé, íî íå àêòèâíûé îáðàáîò÷èê îøèáîê. Åñëè âêëþ÷åííûé, íî íåàêòèâíûé îáðàáîò÷èê îøèáîê íàéäåí íå áóäåò, îøèáêà ñòàíîâèòñÿ ôàòàëüíîé â òîì ìåñòå ïðîãðàììû, â êîòîðîì îíà âïåðâûå âîçíèêëà. Ïðè êàæäîé ïåðåäà÷å óïðàâëåíèÿ îáðàáîò÷èêîì îøèáîê â âûçûâàþùóþ ïðîöåäóðó ýòà ïðîöåäóðà ñòàíîâèòñÿ òåêóùåé. Ïîñëå çàâåðøåíèÿ îáðàáîòêè îøèáêè îáðàáîò÷èêîì â ëþáîé ïðîöåäóðå âîçîáíîâëÿåòñÿ âûïîëíåíèå òåêóùåé ïðîöåäóðû ñ òîé åå ÷àñòè, êîòîðàÿ óêàçàíà â èíñòðóêöèè Resume. Ïîäïðîãðàììà îáðàáîòêè îøèáîê íå ìîæåò áûòü ïðîöåäóðîé Sub èëè Function. Ýòà ïîäïðîãðàììà äîëæíà áûòü ÷àñòüþ ïðîãðàììû, êî