VBA - Whitespace

Creating code is only part of being a good programmer - creating code that is easy to read and follow is key to being a good programmer.

Here is an example code that uses whitespace poorly. Can you work out quickly what will be output?

Now here is the same code but we have used white space to indent to blocks of code are are contained in the if statement. It is clearer to follow the nesting of the if statements now. This same method is useful when using loops. 

Code Snippet:

Sub a7_whitespace2()

Dim x As Integer
Dim y As Integer
Dim z As Integer

x = 4
y = 102
z = -2

If x < 0 Then
    MsgBox "message 1"
    If z < 5 Then
        MsgBox "message 2"
    End If
    If y > 23 Then
       MsgBox "message 3"
        MsgBox "message 4"
    End If
End If
End Sub

Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.

Must Read Articles

VBA and Microsoft Excel - Getting the most out of excel with VBA programming
VBA and Microsoft Word - Getting the most out of word with VBA programming
Application Object - Using the applications features in your code.
Installing VBA and First Program - What do you need to start using visual basic application VBA.