18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Part VI<br />

Programming Excel with VBA<br />

The For-Next loop examines each cell in the InputRange. If the cell has a formula, then the cell’s address<br />

and formula are written to the OutputSheet. The OutputRow variable is also incremented.<br />

Figure 39.12 shows the result of running this macro — a handy list of all formulas in the worksheet.<br />

FIGURE 39.12<br />

The ListFormulas macro creates a list of all formulas in a worksheet.<br />

As macros go, this example is okay, but it’s certainly not perfect. It’s not very flexible, and it doesn’t include<br />

any error handling. For example, if the workbook structure is protected, trying to add a new sheet will<br />

cause an error.<br />

Learning More<br />

This chapter barely scratches the surface of what you can do with VBA. If this is your first exposure to VBA,<br />

you’re probably a bit overwhelmed by objects, properties, and methods. I don’t blame you. If you try to<br />

access a property that an object doesn’t have, you get a run-time error, and your VBA code grinds to a<br />

screeching halt until you correct the problem. Fortunately, several good ways are available to learn about<br />

objects, properties, and methods.<br />

n<br />

n<br />

n<br />

n<br />

Read the rest of the book. Subsequent chapters in this section contain additional information<br />

and many more examples.<br />

Record your actions. The best way to become familiar with VBA is to turn on the macro recorder<br />

and record actions that you make in Excel. You can then examine the code to gain some insights<br />

regarding the objects, properties, and methods.<br />

Use the Help system. The main source of detailed information about Excel’s objects, methods,<br />

and procedures is the VBA Help system. Help is very thorough and easy to access. When you’re in<br />

a VBA module, just move the cursor to a property or method and press F1. You get help that<br />

describes the word that is under the cursor.<br />

Get another book. Several books are devoted exclusively to using VBA with Excel. My book,<br />

Excel 2007 Power Programming with VBA (Wiley Publishing, Inc.), is one of them.<br />

702

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

Saved successfully!

Ooh no, something went wrong!