
For Each Next Loops loop through a collection of items.There are really two types of For Next Loops. If ws.Range("A1").Value = "ABC Global Co." Then The following macro only unhides sheets that have the phrase “ABC Global Co.” in cell A1 of each sheet, and hides all other sheets.

Of course we can use logical statements like If statements to test properties and conditions before performing the actions. The loop starts at the first item in the collection (the first sheet in the workbook), and performs the line(s) of code between the For and Next lines for each item in the collection (every sheet in the workbook). The example below contains a For Next Loop that loops through each worksheet in the workbook and unhides each sheet. The job of the For Next Loop is to perform the same actions (lines of code) on each item in the collection.

Tasks that can take hours to do manually can be completed in a matter of seconds with a loop. We can use loops in our VBA macros to repeat actions very quickly. These tasks can be extremely time consuming and BORING!įortunately, there is a way out. This could be actions like: formatting multiple ranges, unhiding multiple sheets, copying and pasting to several workbooks, apply filters to multiple tables or pivot tables, replacing values, updating formulas, etc.Ĭan you think of a few tasks where you had to repeat the same process over and over again? In Excel we spend a lot of time repeating simple tasks. Skill level: Intermediate The Power of VBA Loops This article explains how the loop works to repeat actions on a collection of items, which saves us a ton of time with our jobs. Bottom line: The For Next Loops are some of the most powerful VBA macro coding techniques for automating common tasks in Excel.
