{"id":4516,"date":"2019-08-05T14:12:39","date_gmt":"2019-08-05T14:12:39","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=4516"},"modified":"2024-03-29T23:56:42","modified_gmt":"2024-03-29T23:56:42","slug":"exit-if-statement","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/","title":{"rendered":"VBA Exit If Statement"},"content":{"rendered":"\n

If you operating with loops, there is a way you can leave\nthe loop based on certain criteria. You can use Exit For<\/strong> or Exit Do<\/strong>, but\nthere is no Exit If<\/strong>.<\/p>\n\n\n\n

If the statement doesn\u2019t represent a loop, but a conditional\nstatement, therefore it doesn\u2019t offer such a construct.<\/p>\n\n\n\n

It doesn\u2019t mean you can\u2019t simulate it.<\/p>\n\n\n\n

In many languages, there is a GoTo<\/strong> statement. It\u2019s not recommended to use GoTo<\/strong> because it makes code harder to read and you can always modify\nyour code, so there is no need to use it.<\/p>\n\n\n\n

But there is GoTo, so I\u2019ll tell you how to implement it into\nyour code.<\/p>\n\n\n\n

In this example, you can see how this code looks like.<\/p>\n\n\n\n

Sub SimulateExitIf()\n    Dim myvar As Integer\n     \n    myvar = 4\n     \n    If myvar < 6 Then\n        GoTo flag1\n    End If\n     \nflag1:\n    MsgBox \"The number is lower than 6\"\nEnd Sub<\/code><\/pre>\n\n\n\n

Excel checks whether the myvar<\/strong> variable is lower than 6. If it\u2019s lower than the GoTo\nstatement is executed and moves interpreter to flag1<\/strong>. In other words: It leaves the If<\/strong> statement.<\/p>\n","protected":false},"excerpt":{"rendered":"

If you operating with loops, there is a way you can leave the loop based on certain criteria. You can use Exit For…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[190],"yoast_head":"\nVBA Exit If Statement<\/title>\n<meta name=\"description\" content=\"Learn how to simulate the Exit If statement in your VB code, even though it doesn't exist, using the GoTo statement. Code example provided.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"VBA Exit If Statement\" \/>\n<meta property=\"og:description\" content=\"Learn how to simulate the Exit If statement in your VB code, even though it doesn't exist, using the GoTo statement. Code example provided.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-05T14:12:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-29T23:56:42+00:00\" \/>\n<meta name=\"author\" content=\"Tomasz Decker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tomasz Decker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/\"},\"author\":{\"name\":\"Tomasz Decker\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"headline\":\"VBA Exit If Statement\",\"datePublished\":\"2019-08-05T14:12:39+00:00\",\"dateModified\":\"2024-03-29T23:56:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/\"},\"wordCount\":161,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"keywords\":[\"pinterest\"],\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/\",\"name\":\"VBA Exit If Statement\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"datePublished\":\"2019-08-05T14:12:39+00:00\",\"dateModified\":\"2024-03-29T23:56:42+00:00\",\"description\":\"Learn how to simulate the Exit If statement in your VB code, even though it doesn't exist, using the GoTo statement. Code example provided.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"VBA Exit If Statement\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"VBA Exit If Statement","description":"Learn how to simulate the Exit If statement in your VB code, even though it doesn't exist, using the GoTo statement. Code example provided.","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:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/","og_locale":"en_US","og_type":"article","og_title":"VBA Exit If Statement","og_description":"Learn how to simulate the Exit If statement in your VB code, even though it doesn't exist, using the GoTo statement. Code example provided.","og_url":"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/","article_published_time":"2019-08-05T14:12:39+00:00","article_modified_time":"2024-03-29T23:56:42+00:00","author":"Tomasz Decker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tomasz Decker","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/"},"author":{"name":"Tomasz Decker","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"headline":"VBA Exit If Statement","datePublished":"2019-08-05T14:12:39+00:00","dateModified":"2024-03-29T23:56:42+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/"},"wordCount":161,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"keywords":["pinterest"],"articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/","url":"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/","name":"VBA Exit If Statement","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"datePublished":"2019-08-05T14:12:39+00:00","dateModified":"2024-03-29T23:56:42+00:00","description":"Learn how to simulate the Exit If statement in your VB code, even though it doesn't exist, using the GoTo statement. Code example provided.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/exit-if-statement\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"VBA Exit If Statement"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/4516"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=4516"}],"version-history":[{"count":3,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/4516\/revisions"}],"predecessor-version":[{"id":12922,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/4516\/revisions\/12922"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=4516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=4516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=4516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}