18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

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

Part VI<br />

Programming Excel with VBA<br />

More about recording VBA macros<br />

If you followed along with the preceding examples, you should have a better feel for how to record macros.<br />

If you find the VBA code confusing, don’t worry. You don’t really have to be concerned with it as long as the<br />

macro that you record works correctly. If the macro doesn’t work, rerecording the macro rather than editing<br />

the code often is easier.<br />

A good way to learn about what gets recorded is to set up your screen so that you can see the code that is<br />

being generated in the Visual Basic Editor windows. To do so, make sure that Excel’s Window isn’t maximized;<br />

then arrange the Excel window and the VB Editor window so both are visible. While you’re recording<br />

your actions, make sure that the VB Editor window is displaying the module in which the code is being<br />

recorded. (You may have to double-click the module name in the Project window.)<br />

TIP<br />

If you do a lot of work with VBA, consider adding a second monitor to your system. Then you<br />

can display Excel on one monitor and the VB Editor on the other.<br />

Absolute versus relative recording<br />

If you’re going to work with recorded macros, you need to understand the concept of relative versus<br />

absolute recording. Normally, when you record a macro, Excel stores exact references to the cells that you<br />

select. (That is, it performs absolute recording.) If you select the range B1:B10 while you’re recording a<br />

macro, for example, Excel records this selection as<br />

Range(“B1:B10”).Select<br />

This statement means exactly what it says: “Select the cells in the range B1:B10.” When you invoke the macro<br />

that contains this statement, the same cells are always selected, regardless of where the active cell is located.<br />

You may have noticed that the Developer ➪ Code section of the Ribbon has a control named Use Relative<br />

References. When you click this control, Excel changes its recording mode from absolute (the default) to<br />

relative. When recording in relative mode, selecting a range of cells is translated differently, depending on<br />

where the active cell is located. For example, if you’re recording in relative mode and cell A1 is active,<br />

selecting the range B1:B10 generates the following statement:<br />

ActiveCell.Offset(0, 1).Range(“A1:A10”).Select<br />

This statement can be translated as “From the active cell, move 0 rows down and 1 column right, and then<br />

treat this new cell as if it were cell A1. Now select what would be A1:A10.” In other words, a macro that is<br />

recorded in relative mode starts out by using the active cell as its base and then stores relative references to<br />

this cell. As a result, you get different results, depending on the location of the active cell. When you replay<br />

this macro, the cells that are selected depend on the active cell. This macro selects a range that is 10 rows by<br />

1 column, offset from the active cell by 0 rows and 1 column.<br />

When Excel is recording in relative mode, the Use Relative Reference control appears depressed. To return<br />

to absolute recording, click the Use Relative Reference control again (and it displays its normal, undepressed<br />

state).<br />

NOTE<br />

The recording mode (either absolute or relative) can make a major difference in how your<br />

macro performs. Therefore, understanding the distinction is important.<br />

Storing macros in your Personal Macro Workbook<br />

Most user-created macros are designed for use in a specific workbook, but you may want to use some<br />

macros in all your work. You can store these general-purpose macros in the Personal Macro Workbook so<br />

that they’re always available to you. The Personal Macro Workbook is loaded whenever you start Excel. This<br />

file, named personal.xlsb, doesn’t exist until you record a macro, using Personal Macro Workbook as the<br />

destination.<br />

692

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

Saved successfully!

Ooh no, something went wrong!