{"id":30127,"date":"2023-07-28T18:47:40","date_gmt":"2023-07-28T16:47:40","guid":{"rendered":"https:\/\/www.myexcelonline.com\/?p=30127"},"modified":"2024-03-22T18:23:08","modified_gmt":"2024-03-22T17:23:08","slug":"mastering-vba-loop-in-excel","status":"publish","type":"post","link":"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/mastering-vba-loop-in-excel\/","title":{"rendered":"Master VBA Loop in Excel &#8211; 3 Types of VBA LOOPs"},"content":{"rendered":"<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/DBF35C1D-410C-4D68-83CA-796D5A62959B.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-30243\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/DBF35C1D-410C-4D68-83CA-796D5A62959B-1024x576.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"1024\" height=\"576\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/DBF35C1D-410C-4D68-83CA-796D5A62959B-1024x576.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/DBF35C1D-410C-4D68-83CA-796D5A62959B-300x169.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/DBF35C1D-410C-4D68-83CA-796D5A62959B-768x432.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/DBF35C1D-410C-4D68-83CA-796D5A62959B.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>Excel is an exceptional tool for data management, complex calculation, and data visualization. However, when it comes to<strong> tackling repetitive tasks<\/strong> and elevating Excel to the next level, <strong><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/category\/macros\/\" target=\"_blank\">VBA<\/a> comes to the rescue.<\/strong> <a class=\"wpil_keyword_link\" title=\"VBA\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/101-excel-macros-examples\/\" target=\"_blank\" rel=\"noopener\" data-wpil-keyword-link=\"linked\">VBA<\/a> loop allows you to<strong> repeat the same task over a range of cells until a specific point is attained or a given condition is met<\/strong>.<\/p>\n<p>In this article, we will cover the following topics &#8211;<\/p>\n<div class=\"om-placeholder\">Download the Excel Workbook below to follow along and understand how VBA Loop works in Excel \u2013<br \/>\n<a class=\"IRPP_minimalist icon-download\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-in-Excel.xlsm\" target=\"_blank\"><span class=\"ctaText px-3 text-capitalize\"><strong>download excel workbook<\/strong>\u00a0<\/span><span class=\"postTitle px-3\">VBA-Loop-in-Excel.xlsm<\/span><\/a><\/div>\n<div><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_57_1 counter-flat ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li><a href=\"#\" class=\"scroll-to-download\">Free Practice Workbook<\/a><\/li><li class='ez-toc-page-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/mastering-vba-loop-in-excel\/#Introduction_to_VBA_Loop\" title=\"Introduction to VBA Loop\">Introduction to VBA Loop<\/a><\/li><li class='ez-toc-page-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/mastering-vba-loop-in-excel\/#1-_For_Loop\" title=\"#1- For Loop\">#1- For Loop<\/a><\/li><li class='ez-toc-page-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/mastering-vba-loop-in-excel\/#2_%E2%80%93_Do_Until_Loop\" title=\"#2 &#8211; Do Until Loop\">#2 &#8211; Do Until Loop<\/a><\/li><li class='ez-toc-page-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/mastering-vba-loop-in-excel\/#3_%E2%80%93_Do_While_Loop\" title=\"#3 &#8211; Do While Loop\">#3 &#8211; Do While Loop<\/a><\/li><li class='ez-toc-page-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/mastering-vba-loop-in-excel\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Introduction_to_VBA_Loop\"><\/span><strong><span id=\"1\" style=\"color: #ff0000\">Introduction to VBA Loop<\/span><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>VBA (Visual Basic for Applications) is a very <strong>valuable and powerful programming language<\/strong> in Excel. The VBA loop enables you to <strong>repetitively execute a task<\/strong> across a range of cells until a specific condition is reached or a given criterion is satisfied.<\/p>\n<p>VBA Loop can be used to<strong> automate your work<\/strong> with the help of just a few lines of code. By sparing you from writing repetitive code and <strong>offering flexibility<\/strong>, it streamlines your work processes. It also helps you <strong>save time and eliminates the potential for human errors<\/strong>.<\/p>\n<p>There are <strong>3 main types<\/strong> of <a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/loop-through-defined-ranges\/\" target=\"_blank\" rel=\"noopener\">VBA loops in Excel<\/a> &#8211;<\/p>\n<ul>\n<li><strong>For Loop:<\/strong> Using a counter to run the code for a <strong>specified number of times<\/strong><\/li>\n<li><strong>Do Until Loop:<\/strong> Looping until a condition is <strong>True<\/strong><\/li>\n<li><strong>Do While Loop:<\/strong>\u00a0Looping while a condition is <strong>True<\/strong><\/li>\n<\/ul>\n<p>Let&#8217;s look at each of these types.<\/p>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"1-_For_Loop\"><\/span><strong><span id=\"2\" style=\"color: #ff0000\">#1- For Loop<\/span><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The For Loop is used when you <strong>know the exact number of times you want the loop to run<\/strong>. It is ideal for<strong> iterating over a range of cells or performing calculations a specific number of times<\/strong>.<\/p>\n<p>The syntax of the loop includes a <strong>counter variable<\/strong> that <strong>begins with the starting value<\/strong> and <strong>repeats<\/strong> through the loop <strong>until it reaches the end value specified by the user<\/strong>.<\/p>\n<p>In this example, we will use a <a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/vba-for-loops\/\" target=\"_blank\" rel=\"noopener\">For Loop in Excel VBA<\/a> to<strong> insert serial numbers 1 to 10 in cells A1 to A10<\/strong>. The loop will run from 1 to 10, and each value will be inserted into the respective cells. Follow the <strong>steps<\/strong> below &#8211;<\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 1:<\/span><\/strong> Open the Workbook and press <strong>Alt + F11<\/strong> to open the VBA editor.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-1.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30133 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-1.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"730\" height=\"404\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-1.png 730w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-1-300x166.png 300w\" sizes=\"(max-width: 730px) 100vw, 730px\" \/><\/a><\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 2:<\/span><\/strong> Click on <strong>Insert<\/strong> &gt; <strong>Module<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-2.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30134 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-2.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"840\" height=\"369\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-2.png 840w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-2-300x132.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-2-768x337.png 768w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><\/a><\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 3:<\/span><\/strong> Write the following <strong>code<\/strong> &#8211;<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-3.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30135 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-3.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"1592\" height=\"408\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-3.png 1592w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-3-300x77.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-3-1024x262.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-3-768x197.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-3-1536x394.png 1536w\" sizes=\"(max-width: 1592px) 100vw, 1592px\" \/><\/a><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nSub InsertSerialNumbers()\r\nDim i As Integer\r\nFor i = 1 To 10\r\nCells(i, 1).Value = i\r\nNext i\r\nEnd Sub\r\n<\/pre>\n<ul>\n<li><strong>Sub InsertSerialNumbers()<\/strong> &#8211; This line is used to define the VBA with the name &#8220;InsertSerialNumbers()&#8221;.<\/li>\n<li><strong>Dim i As Integer<\/strong> &#8211; We declare a variable &#8220;i&#8221; as an integer to act as the counter variable.<\/li>\n<li><strong>For i = 1 To 10 &#8211;<\/strong>\u00a0The For Loop starts with &#8220;i&#8221; set to 1 and continues until &#8220;i&#8221; reaches 10.<\/li>\n<li><strong>Cells(i, 1).Value = i<\/strong> &#8211; Cell is used to specify the row and column. In this case, we set the value of the cell in column A (column number 1) and the row number indicated by the value of &#8220;i.&#8221;<\/li>\n<li><strong>Next i <\/strong>&#8211; This marks the end of the loop and once Excel reaches this line the value of i is increased by 1.<\/li>\n<\/ul>\n<p><strong><span style=\"color: #ff0000\">STEP 4:<\/span><\/strong> Press the <strong>Run<\/strong> icon.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-4.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30136 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-4.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"1609\" height=\"389\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-4.png 1609w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-4-300x73.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-4-1024x248.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-4-768x186.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-4-1536x371.png 1536w\" sizes=\"(max-width: 1609px) 100vw, 1609px\" \/><\/a><\/p>\n<p>Serial numbers ranging from 1 to 10 have been successfully added to cells A1 to A10.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-5.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30137 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-5.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"275\" height=\"555\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-5.png 275w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-5-149x300.png 149w\" sizes=\"(max-width: 275px) 100vw, 275px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"2_%E2%80%93_Do_Until_Loop\"><\/span><strong><span id=\"3\" style=\"color: #ff0000\">#2 &#8211; Do Until Loop<\/span><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Do Until loop will<strong> keep repeating its execution until the specified condition is met<\/strong> i.e. it evaluates to be TRUE. As long as the condition <strong>remains false<\/strong>, the Do Until statements will <strong>continue to be executed<\/strong>. Once the condition is <strong>TRUE<\/strong>, <strong>the loop will end<\/strong>.<\/p>\n<p>In this example, we will use a <strong><a href=\"https:\/\/www.youtube.com\/watch?v=yLcQv7Uxlas\" target=\"_blank\" rel=\"nofollow noopener\">Do Until Loop in Excel VBA<\/a> <\/strong>to insert serial numbers 11 to 20\u00a0 in cells B1 to B10.<\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 1:<\/span><\/strong> Click on <strong>Insert<\/strong> &gt; <strong>Module<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-6.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30138 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-6.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"429\" height=\"420\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-6.png 429w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-6-300x294.png 300w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><\/a><\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 2:<\/span><\/strong> Write the following <strong>code<\/strong> &#8211;<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-7.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30139 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-7.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"1613\" height=\"421\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-7.png 1613w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-7-300x78.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-7-1024x267.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-7-768x200.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-7-1536x401.png 1536w\" sizes=\"(max-width: 1613px) 100vw, 1613px\" \/><\/a><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nSub InsertSerialNumbers()\r\nDim i As Integer\r\ni = 11\r\nDo Until i &amp;amp;amp;amp;amp;amp;amp;gt; 20\r\nCells(i - 10, 2).Value = i\r\ni = i + 1\r\nLoop\r\nEnd Sub\r\n<\/pre>\n<ul>\n<li><strong>i = 11<\/strong> &#8211; We assign the value 11 to &#8220;i&#8221; before entering the loop.<\/li>\n<li><strong>Do Until i &gt; 20<\/strong> &#8211; The loop begins here with the starting value as 1 and keeps running until the value is greater than 10.<\/li>\n<li><strong>Cells(i &#8211; 10, 2).Value = i<\/strong> &#8211; Cell is used to specify the row and column. In this case, we set the value of the cell in column B (column number 2) and the row number indicated by the value of &#8220;i -10.&#8221; In the 1st iteration, this line will be <strong>Cells(1,2).Value = 11<\/strong>.<\/li>\n<li><strong>i = i + 1<\/strong> &#8211; Once Excel reaches this line, the value of i is increased by 1. After the 1st iteration, it will become 12.<\/li>\n<li><strong>Loop<\/strong> &#8211;\u00a0This marks the end of the loop.<\/li>\n<\/ul>\n<p><strong><span style=\"color: #ff0000\">STEP 3:<\/span><\/strong> Press the <strong>Run<\/strong> icon.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-12.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30144 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-12.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"1613\" height=\"421\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-12.png 1613w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-12-300x78.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-12-1024x267.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-12-768x200.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-12-1536x401.png 1536w\" sizes=\"(max-width: 1613px) 100vw, 1613px\" \/><\/a><\/p>\n<p>Serial numbers ranging from 11 to 20 have been successfully added to cells B1 to B10.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-8.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30140 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-8.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"350\" height=\"576\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-8.png 350w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-8-182x300.png 182w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"3_%E2%80%93_Do_While_Loop\"><\/span><strong><span id=\"4\" style=\"color: #ff0000\">#3 &#8211; Do While Loop<\/span><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Do While loop will<strong> keep repeating its execution while the specified condition is met<\/strong>. As long as the condition <strong>remains true<\/strong>, the Do While statements will <strong>continue to be executed<\/strong>. Once the condition is <strong>FALSE or is not met<\/strong>, <strong>the loop will end<\/strong>.<\/p>\n<p>In this example, we will use a Do While Loop in Excel VBA to insert serial numbers 3 to 30\u00a0 in cells C1 to C10, where each number is a multiple of 3.<\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 1:<\/span><\/strong> Click on <strong>Insert<\/strong> &gt; <strong>Module<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-9.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30141 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-9.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"427\" height=\"428\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-9.png 427w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-9-300x300.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-9-150x150.png 150w\" sizes=\"(max-width: 427px) 100vw, 427px\" \/><\/a><\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 2:<\/span><\/strong> Write the following <strong>code<\/strong> &#8211;<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-10.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30142 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-10.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"1613\" height=\"411\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-10.png 1613w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-10-300x76.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-10-1024x261.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-10-768x196.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-10-1536x391.png 1536w\" sizes=\"(max-width: 1613px) 100vw, 1613px\" \/><\/a><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nSub InsertMultipleOfThree()\r\nDim i As Integer\r\nDim j As Integer\r\ni = 3\r\nj = 1\r\nDo While i &amp;amp;amp;amp;amp;amp;amp;lt;= 30\r\nCells(j, 3).Value = i\r\ni = i + 3\r\nj = j + 1\r\nLoop\r\nEnd Sub\r\n<\/pre>\n<ul>\n<li><strong>i = 3 and j = 1<\/strong> &#8211; We assign the value 3 to &#8220;i&#8221; and 1 to &#8220;j&#8221; before entering the loop.<\/li>\n<li><strong>Do While i &lt;= 30<\/strong> &#8211; The loop begins here with the starting value as 3 and keeps running while the value is less than or equal to 30.<\/li>\n<li><strong>Cells(j, 3).Value = i <\/strong>&#8211; Cell is used to specify the row and column. In this case, we set the value of the cell in column C (column number 3) and the row number indicated by the value of &#8220;j&#8221; In the 1st iteration, this line will be <strong>Cells(1,3).Value = 3<\/strong>.<\/li>\n<li><strong>i = i + 3 and j = j + 1 <\/strong>&#8211; Once Excel reaches this line, the value of i is increased by 3 and the value of j is increased by 1. After the 1st iteration, i will become 6 and j will become 2.<\/li>\n<li><strong>Loop <\/strong>&#8211; This marks the end of the loop.<\/li>\n<\/ul>\n<p><strong><span style=\"color: #ff0000\">STEP 3:<\/span><\/strong> Press the <strong>Run<\/strong> icon.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-13.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30145 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-13.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"1613\" height=\"411\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-13.png 1613w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-13-300x76.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-13-1024x261.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-13-768x196.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-13-1536x391.png 1536w\" sizes=\"(max-width: 1613px) 100vw, 1613px\" \/><\/a><\/p>\n<p>The multiples of 3 ranging from 3 to 30 have been successfully added to cells C1 to C10.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-11.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30143 size-full\" title=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-11.png\" alt=\"Master VBA Loop in Excel - 3 Types of VBA LOOPs\" width=\"353\" height=\"580\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-11.png 353w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Loop-11-183x300.png 183w\" sizes=\"(max-width: 353px) 100vw, 353px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The article explains how VBA loops in Excel help automate repetitive tasks and elevate data management. It covers three main types of loops: For Loop, Do Until Loop, and Do While Loop. The step-by-step examples illustrate how each loop type works.<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/fornext-statement\" target=\"_blank\" rel=\"nofollow noopener\"><strong>Click here<\/strong><\/a> to learn more about For Loop in Excel.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Excel is an exceptional tool for data management, complex calculation, and data visualization. However, when it comes to tackling repetitive tasks and elevating Excel to the next level, VBA comes to the rescue. VBA loop allows you to repeat the same task over a range of cells until a specific point is attained or a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":30243,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"cos_headline_score":0,"cos_seo_score":0,"cos_headline_text":"Master VBA Loop in Excel - 3 Types of VBA LOOPs","cos_headline_has_been_analyzed":false,"cos_last_analyzed_headline":[],"om_disable_all_campaigns":false},"categories":[56],"tags":[1301,1299,1297,1300,1296,1298],"acf":[],"_links":{"self":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/posts\/30127"}],"collection":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/comments?post=30127"}],"version-history":[{"count":0,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/posts\/30127\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/media\/30243"}],"wp:attachment":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/media?parent=30127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/categories?post=30127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/tags?post=30127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}