Indenting for Readability
For beginning programmers, one of the most common causes of confusion when using these control structures is keeping track of which statements belong to which If...Then test or Do...While loop. This is particularly true if you end up with control structures nested within other control structures. Programming principle once again here:
- In the main test or loop, indent the statements once (press Tab at the beginning of the first statement).
- In a secondary (that is, nested) test or loop, double-indent the statements (press Tab again at the beginning of the first statement).
- In a tertiary (that is, nested within a nested structure) test or loop, triple-indent the statements (press Tab again at the beginning of the first statement).
Here's a general example of how this indenting looks:
If MainExpression Then If SecondaryExpression Then If TertiaryExpression Then [TertiaryTrueStatements] Else [TeriaryFalseStatements] End If Else [SecondaryFalseStatements] EndIf Else [MainFalseStatements] End If
In this tutorial:
- Controlling Your VBA Code
- Code That Makes Decisions
- Using If...Then to Make True/False Decisions
- Using If Then Else to Handle a False Result
- Making Multiple Decisions
- Using the Select Case Statement
- Functions That Make Decisions
- Code That Loops
- Using Do..Loop Structures
- Using For...Next Loops
- Using For Each...Next Loops
- Using Exit For or Exit Do to Exit a Loop
- Indenting for Readability