{"id":871,"date":"2018-06-30T18:38:52","date_gmt":"2018-06-30T18:38:52","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=871"},"modified":"2024-03-16T13:26:06","modified_gmt":"2024-03-16T13:26:06","slug":"loops","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/training\/loops\/","title":{"rendered":"Loops"},"content":{"rendered":"\n

VBA, as well as other programming languages, offers loops. Looping is an operation of repeating the same block of code multiple times. Excel offers a few types of loops, each best in different types of situations.<\/p>\n\n\n\n

For Next Loop<\/h2>\n\n\n\n

This is the most common type of loop. In the FOR<\/strong> loop, you specify the value where the loop starts and where it finishes. The code between the FOR<\/strong> and NEXT<\/strong> statement gets repeated. Look at the following example.<\/p>\n\n\n\n

For value = 0 To 5\n    MsgBox (value)\nNext value<\/pre>\n\n\n\n

In the above example, the MsgBox<\/em> will display the message 6 times, displaying numbers 0, 1, 2, 3, 4, and 5.<\/p>\n\n\n\n

Additionally, you can skip some values by using the Step<\/strong> value.<\/p>\n\n\n\n

For value = 0 To 5 Step 2\n    MsgBox (value)\nNext value<\/pre>\n\n\n\n

This time the MsgBox<\/em> displays numbers: 0, 2, 4. Because the loop counts to 5 it won\u2019t get to number 6.<\/p>\n\n\n\n

In the following example, we have the array with the four names of the movies. This VBA code will display each name inside the MsgBox<\/em> window.<\/p>\n\n\n\n

Dim movies(1 To 4) As String\n \nmovies(1) = \"Matrix\"\nmovies(2) = \"Lord of the Rings\"\nmovies(3) = \"Star Wars\"\nmovies(4) = \"The Shawshank Redemption\"\n \nFor Count = 1 To 4\n    MsgBox (movies(Count))\nNext Count<\/pre>\n\n\n\n

In the next lesson, I will present you with a similar example with the FOR..EACH..NEXT<\/strong> loop.<\/p>\n\n\n\n

For Each Next Loop<\/h2>\n\n\n\n

In Excel VBA, you can use the FOR..EACH..NEXT<\/strong> loop. It loops through each object in a collection. It can be the collection of worksheets or a collection of elements in an array as presented in our example below.<\/p>\n\n\n\n

Dim movies(1 To 4) As String\nDim movie As Variant\n \nmovies(1) = \"Matrix\"\nmovies(2) = \"Lord of the Rings\"\nmovies(3) = \"Star Wars\"\nmovies(4) = \"The Shawshank Redemption\"\n \nFor Each movie In movies\n    MsgBox (movie)\nNext movie<\/pre>\n\n\n\n

CAUTION<\/h3>\n\n\n\n

You cannot declare the movie<\/strong> variable in the FOR..EACH..NEXT<\/strong> loop as String.<\/strong> It has to always be declared as a Variant.<\/strong><\/p>\n\n\n\n

Do While Loop<\/h2>\n\n\n\n

Unlike the FOR <\/strong>loop, the DO..WHILE<\/strong> a loop doesn\u2019t have specified: start and end values, instead it loops as long as the condition is true.<\/p>\n\n\n\n

Look at the following example:<\/p>\n\n\n\n

Dim counter As Integer\ncounter = 0\n \nDo While counter < 5\n    MsgBox (counter)\n    counter = counter + 1\nLoop<\/pre>\n\n\n\n

In this example, Excel checks the first value, which is 0. The condition is met, so the iteration of the loop is executed. The MsgBox<\/em> window displays the value 0. The next values that also meet the condition are 0, 1, 2, 3, and 4. When counter = 5<\/em> then the condition is not met and the loop terminates.<\/p>\n\n\n\n

Do Until Loop<\/h2>\n\n\n\n

The DO..UNTIL<\/strong> loop is very similar to the DO..WHILE<\/strong> loop. The difference between these two loops is that in the DO..WHILE<\/strong> loop the code is executed as long as the condition is True<\/strong> and in the DO..UNTIL<\/strong> loop it is executed as long as the condition is False.<\/strong><\/p>\n\n\n\n

Dim counter As Integer\ncounter = 0\n \nDo Until counter = 5\n    MsgBox (counter)\n    counter = counter + 1\nLoop<\/pre>\n\n\n\n

The MsgBox<\/em> will display 0, 1, 2, 3, 4. When counter = 5 then the condition is met and the loop terminates.<\/p>\n","protected":false},"excerpt":{"rendered":"

VBA, as well as other programming languages, offers loops. Looping is an operation of repeating the same block of code multiple times. Excel…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[188],"yoast_head":"\nLoops<\/title>\n<meta name=\"description\" content=\"Learn about Excel VBA loops, including the FOR loop, which is the most commonly used to repeat a block of code a specified number of times.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/training\/loops\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Loops\" \/>\n<meta property=\"og:description\" content=\"Learn about Excel VBA loops, including the FOR loop, which is the most commonly used to repeat a block of code a specified number of times.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/training\/loops\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-30T18:38:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-16T13:26:06+00:00\" \/>\n<meta name=\"author\" content=\"Tomasz Decker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tomasz Decker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/training\/loops\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/training\/loops\/\"},\"author\":{\"name\":\"Tomasz Decker\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"headline\":\"Loops\",\"datePublished\":\"2018-06-30T18:38:52+00:00\",\"dateModified\":\"2024-03-16T13:26:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/training\/loops\/\"},\"wordCount\":435,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"keywords\":[\"added\"],\"articleSection\":[\"training\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/training\/loops\/\",\"url\":\"https:\/\/officetuts.net\/excel\/training\/loops\/\",\"name\":\"Loops\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"datePublished\":\"2018-06-30T18:38:52+00:00\",\"dateModified\":\"2024-03-16T13:26:06+00:00\",\"description\":\"Learn about Excel VBA loops, including the FOR loop, which is the most commonly used to repeat a block of code a specified number of times.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/training\/loops\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/training\/loops\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/training\/loops\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Loops\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Loops","description":"Learn about Excel VBA loops, including the FOR loop, which is the most commonly used to repeat a block of code a specified number of times.","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:\/\/officetuts.net\/excel\/training\/loops\/","og_locale":"en_US","og_type":"article","og_title":"Loops","og_description":"Learn about Excel VBA loops, including the FOR loop, which is the most commonly used to repeat a block of code a specified number of times.","og_url":"https:\/\/officetuts.net\/excel\/training\/loops\/","article_published_time":"2018-06-30T18:38:52+00:00","article_modified_time":"2024-03-16T13:26:06+00:00","author":"Tomasz Decker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tomasz Decker","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/training\/loops\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/training\/loops\/"},"author":{"name":"Tomasz Decker","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"headline":"Loops","datePublished":"2018-06-30T18:38:52+00:00","dateModified":"2024-03-16T13:26:06+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/training\/loops\/"},"wordCount":435,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"keywords":["added"],"articleSection":["training"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/training\/loops\/","url":"https:\/\/officetuts.net\/excel\/training\/loops\/","name":"Loops","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"datePublished":"2018-06-30T18:38:52+00:00","dateModified":"2024-03-16T13:26:06+00:00","description":"Learn about Excel VBA loops, including the FOR loop, which is the most commonly used to repeat a block of code a specified number of times.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/training\/loops\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/training\/loops\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/training\/loops\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Loops"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/871"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=871"}],"version-history":[{"count":4,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/871\/revisions"}],"predecessor-version":[{"id":13840,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/871\/revisions\/13840"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}