Excel VBA Coding Tips

When you write your VBA code, remember to write it in such a way that any other user can easily read and understand your code. Even after some months, it might be impossible for you to understand your own code if you don’t use indenting and blank lines to make the code more readable. It is an excellent practice for programmers.

When you press Enter after finishing a line of your VBA code, Excel starts evaluating that line for syntax errors. If no syntax error is found, the line of code will be reformatted, and keywords and identifiers will be in different colors. This automatic reformatting adds consistent spaces (before and after an equal sign, for example) and removes extra spaces that aren’t needed. If a syntax error is found, you receive a pop-up message, and the line is displayed in a different color (red, by default). You need to correct your error before you can execute the macro.

A single statement can be as long as you need. Sometimes it may happen that your statement line is big enough and you want to break the statement into two or more lines. Insert a space followed by an underscore (_) to do so. The following code, although written in two lines, is actually a single VBA statement:

Sheets(“Sheet1”).Range(“B1”).Value = _

You can insert comments freely into your VBA code. The comment indicator is an apostrophe single quote character (‘). Any text that follows a single quote on that line is ignored. A comment can be a line by itself, or it can be inserted after a statement. The following examples show two comments:
‘Assign the values to the variables
Rate = .085 ‘Rate as of November 16

Happy Excelling 🙂

Related Articles




Hello! Welcome to my Excel blog! It took me some time to be a fan of Excel. But now I am a die-hard fan of MS Excel. I learn new ways of doing things with Excel and share them here. Not only how-to guide on Excel, but you will get also topics on Finance, Statistics, Data Analysis, and BI. Stay tuned! You can checkout my courses at Udemy: https://www.udemy.com/user/exceldemy/

We will be happy to hear your thoughts

Leave a reply