Chapter 24

Ten VBA Do’s and Don’ts

IN THIS CHAPTER

Bullet Discovering five VBA-related things you should do

Bullet Discovering five VBA-related things you should not do

If you're reading this final chapter, you’ve probably 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.

If you lack self-discipline, 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. Not declaring all variables has only one advantage: You save a few seconds. But using undeclared ...

Get Excel VBA Programming For Dummies, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.