Indentation and White Space
White space is ignored in VBA and the editor will automatically discard any extra spaces.
If you type
myInteger = 6
This will be automatically changed to
myInteger = 6
The exception is when whitespace is included within double quotes, as part of a string.
You can use the Tab key to indent your lines of code that logically belong together to make the code easier to read.
The tab key should be used instead of pressing the spacebar multiple times.
You can also use (Shift + Tab) to "unindent" your lines of code.
You can change the default tab size from the (Tools > Options)(Editor tab).
Function DoSomething() As Integer
Dim iNumber As Integer
For iNumber = 1 to 5
DoSomething = DoSomething + iNumber
Next iNumber
End Sub
The Indent and Outdent buttons on the Edit toolbar can also be used on blocks of code.
Indent - Shifts all the lines (in the current block) to the next tab stop. | |
Outdent - Shifts all the lines (in the current block) to the previous tab stop. |
Important
All code should be consistently indented with line spaces where appropriate.
Different parts of the code should be separated by using a blank line.
Indentation should be used to highlight the block structure and control statements.
Tabs must be used for indentation (4 which is the default).
One statement per line. Avoid putting multiple statements on the same line (ie using :). This makes debugging more difficult as the project might have to be reset after making dynamic changes. The only exception to this is if you are initialising a simple variable.
Smart Indenter Add-in
link - officeaddins.co.uk/SmartIndenter
The Smart Indenter is an add-in for the Visual Basic Editor that can be used to tidy up VBA code by rebuilding the indentation of each line.
It will work with 64-bit and 32-bit Office and was last updated in 2024.
The source code was provided to the Rubberduck project and then ported to C#.
link - rubberduckvba.com/features
link - github.com/rubberduck-vba/Rubberduck
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext