O'Reilly logo

Excel VBA Programming For Dummies, 3rd Edition by John Walkenbach

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 24

Ten VBA Do’s and Don’ts

In This Chapter

arrow Discovering five VBA-related things you should do

arrow 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 “Option Explicit” 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 of time. But using undeclared variables ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required