MS-Excel / General Formatting

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
[Previous] [Contents]