{"id":137,"date":"2015-10-24T12:18:05","date_gmt":"2015-10-24T12:18:05","guid":{"rendered":"http:\/\/99excel.com\/?p=137"},"modified":"2021-05-14T16:09:21","modified_gmt":"2021-05-14T16:09:21","slug":"for-loop","status":"publish","type":"post","link":"https:\/\/99excel.com\/home\/for-loop\/","title":{"rendered":"For Loop"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>The For Loop is <strong>perhaps<\/strong> the most useful of all the Loops. It runs a line or lines of code a set amount of times in any increment we set. The default increment is one. As you can see from below you <strong>must<\/strong> use a Variable of the Numeric type to set the amount of Loops it will perform.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Sub My_For()<\/p>\n<p>Dim iMyNumber as Integer<\/p>\n<p>&nbsp;<\/p>\n<p><strong>For<\/strong> iMyNumber= 1 To 100<\/p>\n<p>iMyNumber=1+iMyNumber<\/p>\n<p>Range(&#8220;b2&#8221;).Value = iMyNumber<\/p>\n<p><strong>Next <\/strong>iMyNumber<\/p>\n<p>&nbsp;<\/p>\n<p>End Sub<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The other great part about the <strong>For<\/strong> Loop is we can increment by any Value we like. We do this by using the Step Key word and telling it the Step (or increment) to use. so we could use:<\/p>\n<p>&nbsp;<\/p>\n<p>Sub My_For1()<\/p>\n<p>Dim iMyNumber as Integer<\/p>\n<p>Dim iMyNumber2 as Integer<\/p>\n<p>&nbsp;<\/p>\n<p><strong>For<\/strong> iMyNumber= 1 To 100 Step 2<\/p>\n<p>iMyNumber2 =1+iMyNumber2<\/p>\n<p><strong>Next<\/strong> iMyNumber<\/p>\n<p>&nbsp;<\/p>\n<p>Range(&#8220;b2&#8221;).Value = iMyNumber<\/p>\n<p>&nbsp;<\/p>\n<p>End Sub<\/p>\n<p>By doing this we will Loop through our code <strong>51<\/strong> times instead of 101 times, but the Variable iMyNumber will end up with a Value of 101.<\/p>\n<p>&nbsp;<\/p>\n<p>We could also use the Step Key word to work backwards like below:<\/p>\n<p>Sub My_For2()<\/p>\n<p>Dim iMyNumber as Integer<\/p>\n<p>Dim iMyNumber2 as Integer<\/p>\n<p>&nbsp;<\/p>\n<p><strong>For<\/strong> iMyNumber= 1000 To 1 Step -1<\/p>\n<p>iMyNumber2 =1+iMyNumber2<\/p>\n<p><strong>Next<\/strong> iMyNumber<\/p>\n<p>Range(&#8220;b2&#8221;).Value = iMyNumber<\/p>\n<p>&nbsp;<\/p>\n<p>End Sub<\/p>\n<p>&nbsp;<\/p>\n<p>This would mean that our Variable iMyNumber would end up with a Value of 0 (Zero).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; The For Loop is perhaps the most useful of all the Loops. It runs a line or lines of code a set amount of times in any increment we set. The default increment is one. As you can see from below you must use a Variable of the Numeric type to set the amount &#8230; <a title=\"For Loop\" class=\"read-more\" href=\"https:\/\/99excel.com\/home\/for-loop\/\" aria-label=\"More on For Loop\">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-137","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\/137","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=137"}],"version-history":[{"count":1,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":888,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/137\/revisions\/888"}],"wp:attachment":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}