{"id":1264,"date":"2013-12-11T03:46:11","date_gmt":"2013-12-11T03:46:11","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=1264"},"modified":"2019-04-19T07:43:39","modified_gmt":"2019-04-19T07:43:39","slug":"excel-vba-2010-lesson-14-errors-handling","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/","title":{"rendered":"Excel 2010 VBA Lesson 14: Errors Handling"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">&nbsp;<strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-13-do-loop\/\">[Lesson 13]<\/a>&lt;&lt;<a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-tutorial\/\">[Table of Contents]<\/a>&gt;&gt;<a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vbaa-2010-lesson-15-userform\/\">[Lesson 15]<\/a><\/strong><\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">14.1 Introduction to Error Handling<\/h4>\n\n\n\n<p>Errors often occur when the user <g class=\"gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace\" id=\"6\" data-gr-id=\"6\">enter<\/g> incorrect values into a cell of an Excel <g class=\"gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace\" id=\"7\" data-gr-id=\"7\"><g class=\"gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace\" id=\"5\" data-gr-id=\"5\">spreasheet<\/g> .<\/g> For example, an error occurs when instruct the computer to divide a number by zero. Fortunately, we can write Excel 2010 VBA macro to handle those errors efficiently.<\/p>\n\n\n\n<script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>Another example is the user might enter a text (string) to a box that is designed to handle only numeric values, the computer will not be able to perform an arithmetic calculation for text, therefore, will create an error. These errors are known as synchronous errors. Writing errors handling code should be considered a good practice for Excel 2010 VBA &nbsp;programmers, so do not try to finish a program fast by omitting the errors handling code. However, there should not be too many errors handling code in the program as it creates problems for the programmer to maintain and troubleshoot the program later.<br><script>&lt;br \/>     (adsbygoogle = window.adsbygoogle || []).push({});&lt;br \/><\/script><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">14.2 Writing the Errors Handling Code<\/h4>\n\n\n\n<p>We shall now learn how to write errors handling code in Visual Basic. The syntax for errors handling is\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">On Error GoTo program_label\n<\/pre>\n\n\n\n<p>where program_label is the section of code that is designed by the programmer to handle the error committed by the user. Once an error is detected, the program will jump to the program_label section for error handling. You also need to add the statement Exit Sub to prevent the program from jumping to error handling section even though the inputs were correct.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example 14.1<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub CommandButton1_Click()\nOn Error GoTo err_handler\n num1 = InputBox(\"Enter first number\")\n num2 = InputBox(\"Enter second number\")\n MsgBox num1 \/ num2\n Exit Sub\n\nerr_handler:\n MsgBox \"Invalid division, please try again\"\nEnd Sub\n<\/pre>\n\n\n\n<p>The program will display the error message &#8220;Invalid division, please try again&#8221; if the user enter letters instead of numbers or enter the second number as zero, as shown below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"233\" height=\"155\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg\" alt=\"vba2010_fig14.1\" class=\"wp-image-1272\"\/><\/a><\/figure><\/div>\n\n\n\n<p><br>\n<script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><b>Example 14.2<\/b>:&nbsp;<b>Nested Error Handling Procedure<\/b><\/h4>\n\n\n\n<p>By referring to Example 14.1, it is better to alert the user the types of &nbsp;error he or she has committed, such as entering non-numeric data like letters or enter zero as denominator. &nbsp;It should be placed in the first place as soon as the user input something in the input box. And the error handler label error_handler1 for this error should be placed after the error_handler2 label. This means the second error handling procedure is nested within the first error handling procedure. Notice that you have to put an Exit Sub for the second error handling procedure to prevent to execute the first error handling procedure again. The code is as follow:\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub CommandButton2_Click()\nDim firstNum, secondNum As Double\nOn Error GoTo error_handler1\n firstNum = InputBox(\"Enter first number\")\n secondNum = InputBox(\"Enter second number\")\nOn Error GoTo error_handler2\nM sgBox firstNum \/ secondNum\nExit Sub   \u2018To prevent error handling even the inputs are valid\n\nerror_handler2:\n\nMsgBox \" Error!You attempt to divide a number by zero!Try again!\"\n Exit Sub\nerror_handler1:\n MsgBox \" You are not entering a number! Try again!\"\nEnd Sub\n<\/pre>\n\n\n\n<p>The errors are shown in message boxes below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.21.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"289\" height=\"155\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.21.jpg\" alt=\"vba2010_fig14.2\" class=\"wp-image-1278\"\/><\/a><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14-3.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"155\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14-3.jpg\" alt=\"vba2010_fig14-3\" class=\"wp-image-1279\"\/><\/a><\/figure><\/div>\n\n\n\n<p>Finally, you can use the keyword <strong>Resume Next<\/strong> to prevent error message from appearing and branch back to the section of the program where error occured.\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub CommandButton1_Click()\nOn Error Resume Next\n num1 = InputBox(\"Enter first number\")\n num2 = InputBox(\"Enter second number\")\n MsgBox num1 \/ num2\nEnd Sub\n<\/pre>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><br>\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">&nbsp;&nbsp;<strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-13-do-loop\/\">[Lesson 13]<\/a>&lt;&lt;<a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-tutorial\/\">[Table of Contents]<\/a>&gt;&gt;<a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vbaa-2010-lesson-15-userform\/\">[Lesson 15]<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;[Lesson 13]&lt;&lt;[Table of Contents]&gt;&gt;[Lesson 15] 14.1 Introduction to Error Handling Errors often occur when the user enter incorrect values into a cell of an Excel spreasheet . For example, an error occurs when instruct the computer to divide a number by zero. Fortunately, we can write Excel 2010 VBA macro to handle those errors efficiently. &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel 2010 VBA Lesson 14: Errors Handling&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5012,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-1264","page","type-page","status-publish","hentry","category-errors-handling"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Excel 2010 VBA Lesson 14: Errors Handling - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This lesson illustrates errors handling in excel 2010 VBA macro programming\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel 2010 VBA Lesson 14: Errors Handling - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This lesson illustrates errors handling in excel 2010 VBA macro programming\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-19T07:43:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"233\" \/>\n\t<meta property=\"og:image:height\" content=\"155\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/\",\"url\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/\",\"name\":\"Excel 2010 VBA Lesson 14: Errors Handling - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg\",\"datePublished\":\"2013-12-11T03:46:11+00:00\",\"dateModified\":\"2019-04-19T07:43:39+00:00\",\"description\":\"This lesson illustrates errors handling in excel 2010 VBA macro programming\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#primaryimage\",\"url\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg\",\"contentUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg\",\"width\":233,\"height\":155},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel 2010 VBA Lesson 14: Errors Handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/excelvbatutor.com\/#website\",\"url\":\"https:\/\/excelvbatutor.com\/\",\"name\":\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"description\":\"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Excel 2010 VBA Lesson 14: Errors Handling - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This lesson illustrates errors handling in excel 2010 VBA macro programming","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/","og_locale":"en_US","og_type":"article","og_title":"Excel 2010 VBA Lesson 14: Errors Handling - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This lesson illustrates errors handling in excel 2010 VBA macro programming","og_url":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2019-04-19T07:43:39+00:00","og_image":[{"width":233,"height":155,"url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg","type":"image\/jpeg"}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/","url":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/","name":"Excel 2010 VBA Lesson 14: Errors Handling - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg","datePublished":"2013-12-11T03:46:11+00:00","dateModified":"2019-04-19T07:43:39+00:00","description":"This lesson illustrates errors handling in excel 2010 VBA macro programming","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#primaryimage","url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg","contentUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig14.1.jpg","width":233,"height":155},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-14-errors-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel 2010 VBA Lesson 14: Errors Handling"}]},{"@type":"WebSite","@id":"https:\/\/excelvbatutor.com\/#website","url":"https:\/\/excelvbatutor.com\/","name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/1264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/users\/5012"}],"replies":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/comments?post=1264"}],"version-history":[{"count":56,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/1264\/revisions"}],"predecessor-version":[{"id":3324,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/1264\/revisions\/3324"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=1264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=1264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=1264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}