{"id":374,"date":"2019-01-19T23:17:54","date_gmt":"2019-01-19T23:17:54","guid":{"rendered":"http:\/\/www.vbastring.com\/blog\/?p=374"},"modified":"2019-01-29T04:15:08","modified_gmt":"2019-01-29T04:15:08","slug":"what-is-for-each-loop-in-vba","status":"publish","type":"post","link":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/","title":{"rendered":"What is for each loop in VBA?"},"content":{"rendered":"<p>What is for each loop in VBA?<\/p>\n<p>A for each loop in VBA is a way of interating<br \/>\nthrough a range of cells in your worksheet.<\/p>\n<p>For instance, below is my data, I need to place in a column. <\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba.png?resize=1871%2C218\" alt=\"\" width=\"1871\" height=\"218\" class=\"alignleft size-full wp-image-376\" \/><\/a><\/p>\n<p>Here I am adding a button called &#8220;Put In Column&#8221;<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba2.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba2.png?resize=261%2C249\" alt=\"\" width=\"261\" height=\"249\" class=\"alignleft size-full wp-image-377\" \/><\/a><\/p>\n<p>You will need to assign the &#8220;ForEachLoop&#8221; macro to the new button.<\/p>\n<p>Then I just select the cells on row &#8220;A&#8221;, I want to put into a column and click my button! <\/p>\n<p>Now all my row data is in the column (look at the image):<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba3.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba3.png?resize=1873%2C709\" alt=\"\" width=\"1873\" height=\"709\" class=\"alignleft size-full wp-image-379\" \/><\/a><\/p>\n<p>Here is the code:<\/p>\n<pre class=\"lang:vb decode:true \" >Sub ForEachLoop()\r\n    Dim rngSelection As Range\r\n    Dim rngCell As Range\r\n    \r\n    Dim intRow As Integer\r\n    \r\n    Set rngSelection = Selection.Cells 'Whatever I selected is the Selection\r\n    \r\n    'start at row\r\n    intRow = 3\r\n    \r\n    For Each rngCell In rngSelection\r\n        Range(\"A\" &amp; intRow) = rngCell.Value\r\n        intRow = intRow + 1\r\n    Next\r\n    \r\n    \r\nEnd Sub\r\n<\/pre>\n<p>I know you can use the Copy command and choose the Transpose option of Paste Special,<br \/>\nbut this is an example, and I was actually using this to speed the process of generating the create<br \/>\ntable SQL syntax on CSV files I was receiving.<\/p>\n<p>Watch me do it:<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/MkOTQYHTqQY\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p><a href=\"http:\/\/www.getyourboomback.com\/#_l_2pn\"><img data-recalc-dims=\"1\" height=\"580\" width=\"580\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/leaddyno-client-images.s3.amazonaws.com\/cffa678c5db61bb2476a50e2198c69454765190c\/6bc94a38828c6e5e788052b1673c62aecb0d487b_aminoboosters-YTE-square-2bottles-580x580.png?resize=580%2C580&#038;ssl=1\" \/><\/a><\/p>\n<p>Let me know if you need help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is for each loop in VBA? A for each loop in VBA is a way of interating through a range of cells in your worksheet. For instance, below is my data, I need to place in a column. Here I am adding a button called &#8220;Put In Column&#8221; You will need to assign the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-374","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is for each loop in VBA? - My Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is for each loop in VBA? - My Blog\" \/>\n<meta property=\"og:description\" content=\"What is for each loop in VBA? A for each loop in VBA is a way of interating through a range of cells in your worksheet. For instance, below is my data, I need to place in a column. Here I am adding a button called &#8220;Put In Column&#8221; You will need to assign the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/\" \/>\n<meta property=\"og:site_name\" content=\"My Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-19T23:17:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-01-29T04:15:08+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"headline\":\"What is for each loop in VBA?\",\"datePublished\":\"2019-01-19T23:17:54+00:00\",\"dateModified\":\"2019-01-29T04:15:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/\"},\"wordCount\":160,\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/for-each-loop-vba.png\",\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/\",\"name\":\"What is for each loop in VBA? - My Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/for-each-loop-vba.png\",\"datePublished\":\"2019-01-19T23:17:54+00:00\",\"dateModified\":\"2019-01-29T04:15:08+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/#primaryimage\",\"url\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/for-each-loop-vba.png\",\"contentUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/for-each-loop-vba.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/19\\\/what-is-for-each-loop-in-vba\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is for each loop in VBA?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\",\"name\":\"My Blog\",\"description\":\"My WordPress Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\\\/\\\/vbastring.com\\\/blog\"],\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is for each loop in VBA? - My Blog","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:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/","og_locale":"en_US","og_type":"article","og_title":"What is for each loop in VBA? - My Blog","og_description":"What is for each loop in VBA? A for each loop in VBA is a way of interating through a range of cells in your worksheet. For instance, below is my data, I need to place in a column. Here I am adding a button called &#8220;Put In Column&#8221; You will need to assign the [&hellip;]","og_url":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/","og_site_name":"My Blog","article_published_time":"2019-01-19T23:17:54+00:00","article_modified_time":"2019-01-29T04:15:08+00:00","og_image":[{"url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba.png","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/#article","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/"},"author":{"name":"admin","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"headline":"What is for each loop in VBA?","datePublished":"2019-01-19T23:17:54+00:00","dateModified":"2019-01-29T04:15:08+00:00","mainEntityOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/"},"wordCount":160,"image":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba.png","inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/","url":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/","name":"What is for each loop in VBA? - My Blog","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/#primaryimage"},"image":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba.png","datePublished":"2019-01-19T23:17:54+00:00","dateModified":"2019-01-29T04:15:08+00:00","author":{"@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"breadcrumb":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/#primaryimage","url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba.png","contentUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/for-each-loop-vba.png"},{"@type":"BreadcrumbList","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/19\/what-is-for-each-loop-in-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vbastring.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What is for each loop in VBA?"}]},{"@type":"WebSite","@id":"https:\/\/vbastring.com\/blog\/#website","url":"https:\/\/vbastring.com\/blog\/","name":"My Blog","description":"My WordPress Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vbastring.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/vbastring.com\/blog"],"url":"https:\/\/vbastring.com\/blog\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/comments?post=374"}],"version-history":[{"count":5,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"predecessor-version":[{"id":430,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/374\/revisions\/430"}],"wp:attachment":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}