30.04.2017 Views

4523756273

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 22<br />

Ten VBA Questions (And Answers)<br />

In This Chapter<br />

▶ Storing worksheet function procedures<br />

▶ Limitation of the macro recorder<br />

▶ Speeding up your VBA code<br />

▶ Declaring variables explicitly<br />

▶ Using the VBA line continuation character<br />

The following ten questions (and answers) address some of the most<br />

common issues asked by VBA newcomers.<br />

I created a custom VBA function. When I try to use it in a formula, the formula<br />

displays #NAME?. What’s wrong?<br />

You probably put your function code in the wrong location. VBA code for<br />

worksheet functions must be in a standard VBA module — not in a module<br />

for a sheet or in ThisWorkbook. In the VBE, use Insert➪Module to insert a<br />

standard module. Then cut and paste your code to the new VBA module.<br />

This is a very common mistake, because a Sheet module looks exactly like a<br />

standard VBA module. Resist the temptation to put your code there. Spend<br />

four seconds and choose Insert➪Module.<br />

Can I use the VBA macro recorder to record all my macros?<br />

Only if your macros are very simple. Normally, you use it only to record<br />

simple macros or as a starting point for a more complex macro. The macro<br />

recorder can’t record macros that use variables, looping, or any other type<br />

of program flow constructs. In addition, you cannot record a Function procedure<br />

in the VBA macro recorder. Unfortunately, Excel 2007 refuses to record<br />

many actions that are related to charts and shapes. This oversight was corrected<br />

in Excel 2010.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!