{"id":2580,"date":"2015-05-19T12:04:48","date_gmt":"2015-05-19T12:04:48","guid":{"rendered":"http:\/\/www.microsofttraining.net\/b\/?p=2580"},"modified":"2023-12-31T00:06:54","modified_gmt":"2023-12-31T00:06:54","slug":"loop-through-excel-worksheets-and-workbooks","status":"publish","type":"post","link":"https:\/\/www.stl-training.co.uk\/b\/loop-through-excel-worksheets-and-workbooks\/","title":{"rendered":"Loop Through Excel Worksheets and Workbooks"},"content":{"rendered":"<h2>Using loops to easily make changes across multiple worksheets<\/h2>\n<p>Loops are one of the key tools in <a title=\"What is Excel VBA and why should I learn it?\" href=\"https:\/\/www.stl-training.co.uk\/b\/blog\/excel-training\/what-is-excel-vba-and-why-should-i-learn-it\/\">Excel VBA<\/a> when we need to perform tasks through a number of objects (cells, worksheets, charts, workbooks etc.)\u00a0. \u00a0Here we will look at how to loop through Excel worksheets and workbooks.<\/p>\n<h2>Loop Through Excel Worksheets<\/h2>\n<p><a href=\"https:\/\/www.stl-training.co.uk\/b\/wp-content\/uploads\/2015\/04\/loop-sheets.png\"><img decoding=\"async\" class=\"aligncenter wp-image-2582\" src=\"https:\/\/www.stl-training.co.uk\/b\/wp-content\/uploads\/2015\/04\/loop-sheets.png\" alt=\"Loop Through Excel Worksheets\" width=\"600\" height=\"284\" \/><\/a><\/p>\n<p>Below you will find three examples using different loops but all three will perform exactly the same task.<\/p>\n<h2>The For Each loop<\/h2>\n<ol>\n<li>An object variable (sh) is used and declared as Worksheet to tell Excel that we want store worksheets (the address) in the memory \u00a0of our computer (Dim sh As Worksheet).<\/li>\n<li>The\u00a0<strong>For Each\u00a0<\/strong>loop will loop through each worksheet in the active workbook (For Each sh In ActiveWorkbook.Sheets).<\/li>\n<li>The code will add 500 in A1 in all sheets in the active workbook.<\/li>\n<\/ol>\n<p style=\"padding-left: 210px\">Sub LoopSheets()<br \/>\nDim sh As Worksheet<br \/>\nFor Each sh In ActiveWorkbook.Sheets<br \/>\nsh.Range(&#8220;A1&#8221;).Value = 500<br \/>\nNext sh<br \/>\nEnd Sub<\/p>\n<h2>The For Next loop<\/h2>\n<ol>\n<li>A data variable is used to store a whole number (integer) in the computer&#8217;s memory (Dim iCounter As Integer).<\/li>\n<li>The\u00a0<strong>For Next\u00a0<\/strong>loop is used to loop through all sheets in the active workbook but the loop needs to know how many worksheets there is in the active workbook (ActiveWorkbook.Sheets.Count).<\/li>\n<li>The iCounter variable is used to move through the worksheets and the value 500 is entered in A1 in all worksheets in the active workbook (Sheets(iCounter).Range(&#8220;A1&#8221;).Value =\u00a0500).<\/li>\n<\/ol>\n<p style=\"padding-left: 210px\">Sub LoopSheetst2()<br \/>\nDim iCounter As Integer<br \/>\nFor iCounter = 1 To ActiveWorkbook.Sheets.Count<br \/>\nSheets(iCounter).Range(&#8220;A1&#8221;).Value =\u00a0500<br \/>\nNext iCounter<br \/>\nEnd Sub<\/p>\n<h2>The Do loop<\/h2>\n<ol>\n<li style=\"text-align: left\">A data variable is used to store a whole number (integer) in the computer&#8217;s memory (Dim iCounter As Integer).<\/li>\n<li style=\"text-align: left\">1 is stored in the iCounter variable (iCounter = 1).<\/li>\n<li style=\"text-align: left\">A\u00a0<strong>Do Until\u00a0<\/strong>loop is used to run until criteria is met in this example until the value in the variable iCounter is total number of worksheets in the active workbook plus one (Do Until iCounter = ActiveWorkbook.Worksheets.Count + 1).<\/li>\n<\/ol>\n<p style=\"padding-left: 210px\">Sub LoopSheets3()<br \/>\nDim iCounter As Integer<br \/>\niCounter = 1<br \/>\nDo Until iCounter = ActiveWorkbook.Worksheets.Count + 1<br \/>\nSheets(iCounter).Range(&#8220;A!&#8221;).Value = 500<br \/>\niCounter = iCounter + 1<br \/>\nLoop<br \/>\nEnd Sub<\/p>\n<h2>Loop Workbooks<\/h2>\n<p><a href=\"https:\/\/www.stl-training.co.uk\/b\/wp-content\/uploads\/2015\/04\/loop-workbooks.png\"><img decoding=\"async\" class=\"aligncenter wp-image-2593\" src=\"https:\/\/www.stl-training.co.uk\/b\/wp-content\/uploads\/2015\/04\/loop-workbooks.png\" alt=\"loop workbooks\" width=\"600\" height=\"286\" \/><\/a><\/p>\n<p>Below you will find three examples using different loops but all three will perform exactly the same task but this time the loops will loop through workbooks.<\/p>\n<h2>The For Each loop<\/h2>\n<ol>\n<li>An object variable (wBook) is used and declared as Workbook to tell Excel that we want store workbooks (the address)\u00a0in the memory of our computer (Dim WBook As Workbook).<\/li>\n<li>The\u00a0<strong>For Each\u00a0<\/strong>loop will loop through each open workbook\u00a0 (For Each wBook\u00a0In Workbooks).<\/li>\n<li>The code will add 2\u00a0in A2 in sheet 1 in all open workbooks.<\/li>\n<\/ol>\n<p style=\"padding-left: 210px\">Sub LoopWorkBooks()<br \/>\nDim WBook As Workbook<br \/>\nFor Each WBook In Workbooks<br \/>\nWBook.Sheets(1).Range(&#8220;A2&#8221;).Value = 2<br \/>\nNext WBook<br \/>\nEnd Sub<\/p>\n<h2>The For Next loop<\/h2>\n<ol>\n<li>A data variable is used to store a whole number (integer) in the computer&#8217;s memory (Dim iWB As Integer).<\/li>\n<li>The\u00a0<strong>For Next\u00a0<\/strong>loop is used to loop through all open workbooks\u00a0but the loop needs to know how many open workbooks we have\u00a0(Workbooks.Count).<\/li>\n<li>The iWB variable is used to move through the open workbooks and the value 2\u00a0is entered in A2\u00a0in sheet 1 in all open workbooks (Workbooks(iWB).Sheets(1).Range(&#8220;A2&#8221;).Value =\u00a02).<\/li>\n<\/ol>\n<p style=\"padding-left: 210px\">Sub LoopWorkBooks2()<br \/>\nDim iWB As Integer<br \/>\nFor iWB = 1 To Workbooks.Count<br \/>\nWorkbooks(iWB).Sheets(1).Range(&#8220;A2&#8221;).Value =\u00a02<br \/>\nNext iWB<br \/>\nEnd Sub<\/p>\n<h2>The Do loop<\/h2>\n<ol>\n<li style=\"text-align: left\">A data variable is used to store a whole number (integer) in the computer&#8217;s memory (Dim iCounter As Integer).<\/li>\n<li style=\"text-align: left\">1 is stored in the iCounter variable (iCounter = 1).<\/li>\n<li style=\"text-align: left\">A\u00a0<strong>Do Until\u00a0<\/strong>loop is used to run until the criteria is met in this example until the value in the variable iCounter is total number of open workbooks\u00a0plus one (Do Until iCounter = Workbooks.Count + 1).<\/li>\n<\/ol>\n<p style=\"padding-left: 210px\">Sub LoopWorkBooks3()<br \/>\nDim iCounter As Integer<br \/>\niCounter = 1<br \/>\nDo Until iCounter = Workbooks.Count + 1<br \/>\nWorkbooks(iCounter).Sheets(1).Range(&#8220;A2&#8221;).Value = 2<br \/>\niCounter = iCounter + 1<br \/>\nLoop<br \/>\nEnd Sub<\/p>\n<h2>Loop workbooks &amp; worksheets<\/h2>\n<p><a href=\"https:\/\/www.stl-training.co.uk\/b\/wp-content\/uploads\/2015\/04\/loop-workbooks.png\"><img decoding=\"async\" class=\"alignleft wp-image-2593 size-medium\" src=\"https:\/\/www.stl-training.co.uk\/b\/wp-content\/uploads\/2015\/04\/loop-workbooks-300x143.png\" alt=\"loop workbooks\" width=\"300\" height=\"143\" \/><\/a><\/p>\n<p style=\"padding-left: 210px\"><a href=\"https:\/\/www.stl-training.co.uk\/b\/wp-content\/uploads\/2015\/04\/loop-sheets.png\"><img decoding=\"async\" class=\"alignright wp-image-2582 size-medium\" src=\"https:\/\/www.stl-training.co.uk\/b\/wp-content\/uploads\/2015\/04\/loop-sheets-300x142.png\" alt=\"loop sheets\" width=\"300\" height=\"142\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>x<\/p>\n<h2><\/h2>\n<p>In the examples below nested loops are looping through workbooks and worksheets and again the\u00a0<strong>For Each<\/strong>,\u00a0<strong>For Next\u00a0<\/strong>and the\u00a0<strong>Do<\/strong> loop are used to do the job.<\/p>\n<h2>The For Each loop<\/h2>\n<p>Exactly as in the examples above in this post variables are used to store the address of the workbooks and worksheets in the computers memory (Dim WBook As Workbook &amp;\u00a0Dim sh As Worksheet). A <b>For Each\u00a0<\/b>loop is used to run through the workbooks and one to run through the worksheets.<\/p>\n<p style=\"padding-left: 210px\">Sub\u00a0LoopWorkBookSheets()<br \/>\nDim WBook As Workbook<br \/>\nDim sh As Worksheet<br \/>\nFor Each WBook In Workbooks<br \/>\nFor Each sh In WBook.Worksheets<br \/>\nsh.Range(&#8220;a1&#8221;) = 2<br \/>\nNext sh<br \/>\nNext WBook<br \/>\nEnd Sub<\/p>\n<h2>The For Next loop<\/h2>\n<p>Two\u00a0<strong>For Next\u00a0<\/strong>loops are needed to run through all worksheets in all open workbooks. Two variables are used (counter variables) to loop one workbook at the time and one worksheet.<\/p>\n<p style=\"padding-left: 210px\">Sub LoopWorkBookSheets2()<br \/>\nDim iWB As Integer<br \/>\nDim iCounter As Integer<br \/>\nFor iWB = 1 To Workbooks.Count<br \/>\nFor iCounter = 1 To Workbooks(iWB).Sheets.Count<br \/>\nWorkbooks(iWB).Sheets(iCounter).Range(&#8220;b1&#8221;).Value = 450<br \/>\nNext iCounter<br \/>\nNext iWB<br \/>\nEnd Sub<\/p>\n<h2>The Do loop<\/h2>\n<p>It takes more coding to run through all worksheets in all open workbooks by using the\u00a0<strong>Do<\/strong> loop. Again two loops are needed one for the workbooks and one for the worksheets.<\/p>\n<p style=\"padding-left: 210px\">Sub LoopWorkBookSheets3()<br \/>\nDim iWorkBookCounter As Integer<br \/>\nDim iSheetCounter As Integer<br \/>\niWorkBookCounter = 1<br \/>\niSheetCounter = 1<br \/>\nDo Until iWorkBookCounter = Workbooks.Count + 1<br \/>\nDo Until iSheetCounter = Workbooks(iWorkBookCounter).Sheets.Count + 1<br \/>\nWorkbooks(iWorkBookCounter).Sheets(iSheetCounter).Range(&#8220;c1&#8221;).Value = 5<br \/>\niSheetCounter = iSheetCounter + 1<br \/>\nLoop<br \/>\niWorkBookCounter = iWorkBookCounter + 1<br \/>\niSheetCounter = 1<br \/>\nLoop<br \/>\nEnd Sub<\/p>\n<p>Some people\u00a0prefer to use the\u00a0<strong>For Each\u00a0<\/strong>loop for a couple of reasons. The\u00a0<strong>For Each\u00a0<\/strong>loop is a faster loop and normally you need less coding.<\/p>\n<p>Good luck with your loops!<\/p>\n<p><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;microsoft office 365 training&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:14851,&quot;3&quot;:{&quot;1&quot;:0},&quot;4&quot;:[null,2,16777215],&quot;12&quot;:0,&quot;14&quot;:[null,2,6121321],&quot;15&quot;:&quot;Arial&quot;,&quot;16&quot;:11}\">Excel course is one of our <a href=\"\/syl\/136\/office-365-end-user-training-course.html\">Microsoft Office 365 training<\/a> courses.<\/span><\/p>\n<h3>Useful Resources<\/h3>\n<p><a href=\"https:\/\/support.microsoft.com\/en-us\/kb\/142126\">Macro to Loop Through All Worksheets in a Workbook<\/a><\/p>\n<p><a title=\"Excel VBA 2013: Track Changes With the Inquire Add-In\" href=\"https:\/\/www.stl-training.co.uk\/b\/blog\/vba-training\/excel-vba-2013-track-changes-with-the-inquire-add-in\/\">Excel VBA 2013: Track Changes With the Inquire Add-In<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using loops to easily make changes across multiple worksheets Loops are one of the key tools in Excel VBA when we need to perform tasks through a number of objects (cells, worksheets, charts, workbooks etc.)\u00a0. \u00a0Here we will look at how to loop through Excel worksheets and workbooks. Loop Through Excel Worksheets Below you will [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,19],"tags":[342,343,347],"class_list":["post-2580","post","type-post","status-publish","format-standard","hentry","category-excel-training","category-vba-training","tag-loop-excel-worksheets","tag-loop-through-all-excel-files","tag-macro-to-loop-through-all-spreadsheets"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/posts\/2580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/comments?post=2580"}],"version-history":[{"count":1,"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/posts\/2580\/revisions"}],"predecessor-version":[{"id":3735,"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/posts\/2580\/revisions\/3735"}],"wp:attachment":[{"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/media?parent=2580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/categories?post=2580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stl-training.co.uk\/b\/wp-json\/wp\/v2\/tags?post=2580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}