{"id":129,"date":"2015-10-24T12:16:10","date_gmt":"2015-10-24T12:16:10","guid":{"rendered":"http:\/\/99excel.com\/?p=129"},"modified":"2021-05-14T16:09:21","modified_gmt":"2021-05-14T16:09:21","slug":"use-of-loops","status":"publish","type":"post","link":"https:\/\/99excel.com\/home\/use-of-loops\/","title":{"rendered":"Use of Loops"},"content":{"rendered":"<p><strong>L<\/strong>ooping is one of the most powerful programming techniques. A loop in Excel VBA enables you to loop through a range of cells with just a few codes lines.<\/p>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s look at each one in turn and use them in a simple way.<\/p>\n<p><u>\u00a0<\/u><\/p>\n<p><strong>Do Loop<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>START:<\/strong> \u00a0<strong>Do <\/strong>(tell our Loop)<\/p>\n<p><strong>STOP:<\/strong> <strong>Do While <\/strong>(This is very similar to our Do Loop we just used above. The only difference is, instead of the condition we set (iMyNumber &lt; 1000) being checked AFTER the Do has run at least once, the Do While will check the condition BEFORE it runs. Let&#8217;s say our Variable iMyNumber has already had the Value 1001 parsed to it. In the Do Loop we used above, it will NOT know the Value of iMyNumber until it has run ONCE. This would mean our Variable would come out of the Do Loop with a Value of 1002. But in the Do While below, it would never even enter the Loop)<\/p>\n<p>&nbsp;<\/p>\n<p>Sub My_Do()<\/p>\n<p><strong>Do<\/strong><\/p>\n<p><em>&lt;Code to repeat&gt;<\/em><\/p>\n<p><strong>Loop While<\/strong> <em>&lt;Whatever&gt;<\/em><\/p>\n<p>End Sub<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Progress:<\/strong> Add the number one to a Integer Variable we called\u00a0 &#8220;iMyNumber&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Sub My_Do1 ()<\/p>\n<p>Dim iMyNumber as Integer<\/p>\n<p><strong>Do<\/strong><\/p>\n<p>iMyNumber=1+iMyNumber<\/p>\n<p><strong>Loop While<\/strong> &lt;Whatever&gt;<\/p>\n<p>End Sub<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Progress:<\/strong> Ok, so we have now told our <strong>&#8220;Do&#8221;<\/strong> what it is we want to do. Now we have to tell it when to stop. To achieve this we must set a condition for our <strong>&#8220;Loop While&#8221;<\/strong>. In this case we will: Do<strong> iMyNumber=1+iMyNumber<\/strong> until <strong>iMyNumber is &gt; 1000<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Sub My_Do2 ()<\/p>\n<p>Dim iMyNumber as Integer<\/p>\n<p><strong>Do<\/strong><\/p>\n<p>iMyNumber = 1 + iMyNumber<\/p>\n<p><strong>Loop While<\/strong> iMyNumber &lt; 1000<\/p>\n<p>Range(&#8220;b2&#8221;).Value = iMyNumber<\/p>\n<p>End Sub<\/p>\n<p>&nbsp;<\/p>\n<p>So the bottom line here is the Do Loop will Loop <strong>1001 times<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looping is one of the most powerful programming techniques. A loop in Excel VBA enables you to loop through a range of cells with just a few codes lines. &nbsp; Let&#8217;s look at each one in turn and use them in a simple way. \u00a0 Do Loop &nbsp; START: \u00a0Do (tell our Loop) STOP: Do &#8230; <a title=\"Use of Loops\" class=\"read-more\" href=\"https:\/\/99excel.com\/home\/use-of-loops\/\" aria-label=\"More on Use of Loops\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":1,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":892,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/129\/revisions\/892"}],"wp:attachment":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}