Blue – Code keywords (capitalized)
Red – Wrong use of code
Green – Comments
Comments can be added anywhere in the code space for ease of navigation as long as it starts with a single quotation mark (‘).
These are commonly used to insert brief descriptions of the code for reference.
Split long code into two lines
For readability, a single long line of code can be split into two lines by adding a space and underscore (_) in the middle of a line of code and hitting Enter.
Alignment and Structure
VBE automatically adds an indent for every level to keep things organized.
This can be done manually by pressing Tab, and can be undone by pressing SHIFT + Tab.
This can be applied to multiple lines of code by highlighting them and pressing Tab.
The Indent and the Outdent icons on the Edit Option Toolbar can also be used.
Auto Syntax check
This is useful in alerting you with errors in your code through a popup.
To enable this, go to Tools > Option. Tick the box for Auto Syntax Check.
Helpful when creating your own procedures or modifying codes.
This automatically puts Option Explicit on top of all the modules created.
To activate this, go to Tools > Option.
Tick the box for Require Variable Declaration.
Auto List Members
Aids in having the correct name references in the code by providing a list of members found in the module.
There are three ways to activate this:
- Go to Tools > Option. Tick the box for Auto List Members.
- Under the Edit options toolbar, select the first icon
- CTRL + J
It helps you complete a word in the code by giving you a list of options.
This is enabled through the Edit Options toolbar or by pressing CTRL + Space.
Disabling parts of the code
When testing the code, there might be some lines that need to be disabled.
To do this in a quick manner, highlight the lines and click on Comment Block in the Edit Options Toolbar.
This converts those lines into a block of comments, indicated by the single quotation mark at the start and green color.
This can be switched back by clicking on the icon for Uncomment Block on the Edit Options Toolbar.
This is automatically added to the lines of code when the macro recorder is used.
The Select statement is usually not necessary and can be removed to improve readability.
This is because with VBA, objects don’t need to be selected to manipulate them as long as they are referred to.
This is usually used as a sign of equality between sides, but for VBA, its main function is t o assign the right side of the code to the left side.
Cell and Range References
VBA refers to cells in different ways.
ActiveCell refers to the cell that is currently active or is currently clicked on.
When referencing to a range, it uses syntax that is similar to Excel formulas (e.g. RANGE(“A1:E1”)).