{"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 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":"\nSub 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