In this blog post, we provide you with some basic tools for writing VBA code.
The VBA Editor incorporates a number of useful features which help you whilst you are writing code. Here we’ll take a closer look at a few code writing features of the VBA editor.
Line Continuation Character
When we write code, we are often required to create string expressions that are wider than the page itself! Although the entire string is on one line, it makes it more difficult to code as you constantly have to use the horizontal scroll bar to read exactly what is written.
The smart people at Microsoft came up with a simple plan, the line continuation character, or space and underscore for short. We use this with an ampersand (&) to make our code easier to read.
Another helpful feature when reading code is indentation. Indentation gives us a clear indication of code blocks. Indentation is implicit, so typing in a Public Function Name() and pressing the enter key will add the End Function and indent your code by two or four spaces or a tab. This is basically standard practice across all programming languages.
|Using indentation it is easy to see which bits of code are associated with one another.|
Editor Format to Adjust Colours
The above example also neatly brings us to syntax highlighting. All keywords in VBA are dark blue by standard and anything we write is in a black font (except comments which are green). Syntax highlighting serves to use our sense of colour to add meaning to the code. You can change the colours too! Follow the steps below: Click on the Tools drop down menu, select Options… tab over to Editor Format and change as needed. Working under poor lighting conditions makes the default black on white very uncomfortable, so perhaps a blackened theme would be more suitable.
A naming convention is a way of naming variables and objects which your developer group uses to make your code easier to understand. In the image above, Function showMeIndentation() is in “camelCase “. Here are some additional naming convention details:
- Use meaningful variable names – make your variables mean something. Zzxd isn’t meaningful, but fileNotFound has a semantic meaning for humans (even though it doesn’t affect the computer or VBA in any way).
- Use camelCase for variables and functions – that is, for every word in your variable name make the first letter upper-case, except the first letter of the first word. thisIsCamelCase()
- Use UPPER_CASE for constants – when you declare a constant the name of that constant is usually capitalised. This means nothing to the compiler but means everything to you.
Amongst VBA developers a typical naming convention is to start all variables with a letter indicating the variable type (in lower case):
- iMyNumber would be of type Integer
- dblMyOtherNumber would be of type Double
- sText would be of type String
With form and report controls a three letter prefix is very common also:
But, the point of a naming convention is to make your code more accessible to others by imposing on you and your colleagues a consistent way of writing code. Feel free within your departments or projects to use whatever naming convention you like, but the key is to be consistent.
Select Object Dropdown and Procedures Dropdown
Hidden in plain view are two drop down menus just above the Code Editor Window. The drop down menu on the left is used to select and even indicate which control your cursor is currently in and the one on the right lists all available events for that object.
Procedural View and Full Module View
Another useful feature is the ability to switch between module and procedure view in the code window. Module view is what we normally see but if you have lots of procedures and functions and would like to only view the one you are working on, click the button on the left in the image below . All the other code will magically disappear!
These are some useful tips for writing code. Follow these rules and your code writing will improve significantly.
Related PostsCompilation Explained
Opening The VBA Editor
The VBA Editor Explained
Visual Basic Editor Options