ExcelQ’s Coding Standards
It is a sensible move to document all your VBA programs even if you are an amateur programmer. Here are a few tips on how to accomplish this task in a breeze and make it crystal clear as to what your VBA program is trying to do and why. It is common in most companies for Excel users to write VBA programs. But in most cases, these programs are undocumented and often less than perfect. But this lack of documentation makes it difficult in most cases, even impossible for even the programmer to fathom what the program is trying to do, why it is trying to do it, how the program is supposed to work or even how one might be able to fix any error occurrences with the code.
It would be irrational for managers to prevent the Excel programmers to write VBA codes but it is rational and possible for managers to write VBA programs in a code that can be understood by everyone and not just the programmer. Writing programs that others can read is not only necessary for the business but also very much possible to do in Excel. There are a few VBA standards that are easy to learn and maintain. So y following these standards, you can understand your VBA code easily in the future. This would also allow others to understand the code you've concocted.
- Using Option Explicit- Variables can contain a variety of data ranging from integers, strings, currency etc. and this fact is common knowledge among the Excel VBA programmers. VBA defines all variables using the Variant Data type because it does not require you to specify the type of data each variable is supposed to contain. This unanimity creates a lot of problems and one of the less hyped up problem is the problem of performance. Variants require more memory than other common data and require more time to process. A worse problem is that by using only variants in your codes makes it difficult to pin down errors. Also using variants by default prevent people from reading your codes and understanding their nature. To solve this problem, go to the Visual Basic Editor (VBE), choose tools from options, then check the Require Variable Declaration in the Editor tab. Now each new VBA code that you launch will begin with “option explicit” which means that you have to declare each variable. By declaring your variables, you reduce the chances of error dramatically.
- Hungarian Notation- In the old days of DOS, a method known as Hungarian Notation was introduced as a useful way of naming variables. This method merely adds a prefix to each variable. This prefix identifies the type of variable. This method has also been frequently used to identify the scope of the variable. This makes the VBA codes more readable and understandable.
So by applying these tips and tricks, you can easily create VBA codes that can be read by the programmer as well as other people without it seeming like a foreign novel.