{"id":323,"date":"2013-02-27T10:34:43","date_gmt":"2013-02-27T10:34:43","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=323"},"modified":"2020-04-23T10:54:28","modified_gmt":"2020-04-23T10:54:28","slug":"excel-vba-lesson-15-do-loop","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-15-do-loop\/","title":{"rendered":"Excel VBA Lesson 15: The Do Loop"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-14-looping\/\">&lt;&lt;Lesson 14&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-16-formatting-font-colors\/\">&lt;&lt;Lesson 16&gt;&gt;<\/a><\/strong><\/h4>\n\n\n\n<p>In the previous lesson, you have learned how to use the &nbsp;<b>&nbsp;For&#8230;&#8230;..Next<\/b>&nbsp;loop and the <strong>While&#8230;..Wend<\/strong> loop to execute a repetitive process. In this lesson,&nbsp; you will learn about another looping method known as the&nbsp;<b>Do<\/b>&nbsp;Loop. There are four ways you can use the Do Loop, as shown below:<\/p>\n\n\n\n<p>(i) Do&#8230;&#8230;&#8230;..Loop While<\/p>\n\n\n\n<p>(i) Do&#8230;&#8230;&#8230;..Loop While<\/p>\n\n\n\n<p>(ii) Do until&#8230;&#8230;&#8230;&#8230;.Loop<\/p>\n\n\n\n<p>(iii) Do while&#8230;&#8230;&#8230;&#8230;Loop<\/p>\n\n\n\n<p>(iv) Do&#8230;&#8230;&#8230;&#8230;Loop until<\/p>\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>\nWe shall illustrate the four structures of Do Loops in the following examples:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 15.1<\/strong><\/h4>\n\n\n\n<p>Arranging numbers in ascending order<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click()<br>\nDim counter As Integer<br>\nDo<br>\ncounter = counter + 1<br>\nCells(counter, 1) = counter<br>\nLoop While counter &lt; 10<\/p>\n\n\n\n<p>End Sub<\/p>\n\n\n\n<p>In this example, the program will keep on adding 1 to the preceding counter value as long as the counter value is less than 10. It displays 1 in cells(1,1), 2 in cells(2,1)\u2026.. until 10 in cells (10,1).<\/p>\n\n\n\n<p>The Output<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_1.gif\" alt=\"Excel VBA\"\/><\/figure>\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><\/p>\n\n\n\n<p><strong>Figure 15.1<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 15.2<\/strong><\/h4>\n\n\n\n<p>Arranging numbers in descending order<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click()<br>\nDim counter As Integer<br>\nDo Until counter = 10<br>\ncounter = counter + 1<br>\nCells(counter, 1) = 11 &#8211; counter<br>\nLoop<br>\nEnd Sub<\/p>\n\n\n\n<p>In this example, the program will keep on adding 1 to the preceding counter value until the counter value reaches 10. It displays 10 in cells(1,1), 9&nbsp; in cells(2,1)\u2026.. until 1 in cells (10,1).<\/p>\n\n\n\n<p><strong>The Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_2.gif\" alt=\"Excel VBA\"\/><\/figure>\n\n\n\n<p><strong>Figure 15.2<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 15.3<\/strong><\/h4>\n\n\n\n<p>Private Sub CommandButton1_Click()<br>\nDim counter , sum As Integer<\/p>\n\n\n\n<p><span style=\"color: #008000;\">&#8216;To set the alignment to center<\/span><br>\nRange(&#8220;A1:C11&#8221;).Select<br>\nWith Selection<br>\n.HorizontalAlignment = xlCenter<br>\nEnd With<\/p>\n\n\n\n<p>Cells(1, 1) = &#8220;X&#8221;<br>\nCells(1, 2) = &#8220;Y&#8221;<br>\nCells(1, 3) = &#8220;X+Y&#8221;<\/p>\n\n\n\n<p>Do While counter &lt; 10<br>\ncounter = counter + 1<br>\nCells(counter + 1, 1) = counter<br>\nCells(counter + 1, 2) = counter * 2<br>\nsum = Cells(counter + 1, 1) + Cells(counter + 1, 2)<br>\nCells(counter + 1, 3) = sum<br>\nLoop<br>\nEnd Sub<\/p>\n\n\n\n<p>In this example, the program will display the values of X in cells(1,1) to cells(11,1). The value of Y is X<sup>2<\/sup>and the values are display in column 2, i.e. from cells(2,1) to cells(2,11). Finally, it shows the values of X+Y in column 3, i.e. from cells(3,1) to cells(3,11)<\/p>\n\n\n\n<p><strong>The Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_3.gif\" alt=\"Excel VBA\"\/><\/figure>\n\n\n\n<p><br>\n<strong>Figure 15.3<\/strong><br>\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-14-looping\/\">&lt;&lt;Lesson 14&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-16-formatting-font-colors\/\">&lt;&lt;Lesson 16&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 14&gt;&gt; [Contents] &lt;&lt;Lesson 16&gt;&gt; In the previous lesson, you have learned how to use the &nbsp;&nbsp;For&#8230;&#8230;..Next&nbsp;loop and the While&#8230;..Wend loop to execute a repetitive process. In this lesson,&nbsp; you will learn about another looping method known as the&nbsp;Do&nbsp;Loop. There are four ways you can use the Do Loop, as shown below: (i) Do&#8230;&#8230;&#8230;..Loop While &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-15-do-loop\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 15: The Do Loop&#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":[5,11],"tags":[],"class_list":["post-323","page","type-page","status-publish","hentry","category-decison-making","category-loop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Excel VBA Lesson 15: The Do Loop - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This Excel VBA lesson illustrates the use of Do....Loop in Excel VBA\" \/>\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\/vba_lesson15.htm\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel VBA Lesson 15: The Do Loop - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This Excel VBA lesson illustrates the use of Do....Loop in Excel VBA\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson15.htm\" \/>\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=\"2020-04-23T10:54:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_1.gif\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 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-lesson-15-do-loop\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson15.htm\",\"name\":\"Excel VBA Lesson 15: The Do Loop - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson15.htm#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson15.htm#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_1.gif\",\"datePublished\":\"2013-02-27T10:34:43+00:00\",\"dateModified\":\"2020-04-23T10:54:28+00:00\",\"description\":\"This Excel VBA lesson illustrates the use of Do....Loop in Excel VBA\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson15.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson15.htm\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson15.htm#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_1.gif\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_1.gif\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson15.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 15: The Do Loop\"}]},{\"@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 VBA Lesson 15: The Do Loop - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This Excel VBA lesson illustrates the use of Do....Loop in Excel VBA","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\/vba_lesson15.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 15: The Do Loop - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This Excel VBA lesson illustrates the use of Do....Loop in Excel VBA","og_url":"https:\/\/excelvbatutor.com\/vba_lesson15.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T10:54:28+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_1.gif","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-15-do-loop\/","url":"https:\/\/excelvbatutor.com\/vba_lesson15.htm","name":"Excel VBA Lesson 15: The Do Loop - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson15.htm#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson15.htm#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_1.gif","datePublished":"2013-02-27T10:34:43+00:00","dateModified":"2020-04-23T10:54:28+00:00","description":"This Excel VBA lesson illustrates the use of Do....Loop in Excel VBA","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson15.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson15.htm"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/vba_lesson15.htm#primaryimage","url":"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_1.gif","contentUrl":"https:\/\/www.vbtutor.net\/Images\/progra_jan_25_2008_vbaV_1.gif"},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson15.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 15: The Do Loop"}]},{"@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\/323","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=323"}],"version-history":[{"count":18,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/323\/revisions"}],"predecessor-version":[{"id":3351,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/323\/revisions\/3351"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}