Chapter 25
Ten VBA Do’s and Don’ts
IN THIS CHAPTER
Discovering five VBA-related things you should do
Discovering five VBA-related things you should not do
If you're reading this final chapter, you may have read most of the content of this book and are familiar with Excel VBA. Or, maybe you just skipped to the end because you wanted to see whether the butler actually did it.
This chapter gives you some advice you should take into account when you start developing your own VBA solutions. Following these guidelines is no panacea to keep you out of (programming) trouble, but following them can help you avoid pitfalls that others have stumbled over.
Do Declare All Variables
How convenient it is: Simply start typing your VBA code without having to go through the tedious chore of declaring each and every variable you want to use. Although Excel allows you to use undeclared variables, doing so is simply asking for trouble.
The first commandment of VBA programming should be this:
Thou shalt declare every variable.
To force yourself to obey that commandment, add an “Option Explicit” statement at the top of your modules. That way, your code won’t even run if it includes one or more undeclared variables. In fact, you can have Excel include Option Explicit on any new modules automatically. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access