Chapter 4
Looping and flow control
In this chapter, you will:
Work with
For...Next
loopsGet to know
Do
loopsBe introduced to the VBA loop:
For Each
Use
If...Then...Else
andSelect Case
for flow control
Loops make your life easier. You might have 20 lines of macro code that do something cool one time. Add a line of code above and below, and suddenly your macro fixes a million rows instead of one row. Loops are a fundamental component of any programming language. If you’ve taken any programming classes—even BASIC—you’ve likely encountered a For...Next
loop. Fortunately, VBA supports all the usual loops, plus a special loop that is excellent to use with VBA.
This chapter covers the basic loop constructs:
For...Next
Do...While
Do...Until
Get Microsoft Excel VBA and Macros (Office 2021 and Microsoft 365) now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.