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 />

n<br />

n<br />

n<br />

n<br />

I recommend that you use named ranges in your VBA code. For example, a reference such as<br />

Range (“Total”) is better than Range (“D45”). In the latter case, you need to modify the<br />

macro if you add a row above row 45.<br />

When you record macros that select ranges, pay close attention to relative versus absolute recording<br />

mode (see Chapter 39). The recording mode that you choose can drastically affect the way the<br />

macro operates.<br />

If you create a macro that loops through each cell in the current range selection, be aware that the<br />

user can select entire columns or rows. For example, you may want to eliminate empty cells. In<br />

such a case, you need to create a subset of the selection that consists only of nonblank cells. Or,<br />

you can work with cells in the worksheet’s used range (by using the UsedRange property).<br />

Be aware that Excel allows you to select multiple ranges in a worksheet. For example, you can<br />

select a range, press Ctrl, and then select another range. You can test for this in your macro and<br />

take appropriate actions.<br />

The examples in the following sections demonstrate these points.<br />

Copying a range<br />

Copying a range is a frequent activity in macros. When you turn on the macro recorder (using absolute<br />

recording mode) and copy a range from A1:A5 to B1:B5, you get a VBA macro like this:<br />

Sub CopyRange()<br />

Range(“A1:A5”).Select<br />

Selection.Copy<br />

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

ActiveSheet.Paste<br />

Application.CutCopyMode = False<br />

End Sub<br />

This macro works, but it’s not the most efficient way to copy a range. You can accomplish exactly the same<br />

result with the following one-line macro:<br />

Sub CopyRange2()<br />

Range(“A1:A5”).Copy Range(“B1”)<br />

End Sub<br />

This code takes advantage of the fact that the Copy method can use an argument that specifies the destination.<br />

Useful information about properties and methods is available in the Help system.<br />

The example demonstrates that the macro recorder doesn’t always generate the most efficient code. As you<br />

see, you don’t have to select an object to work with it. Note that CopyRange2 doesn’t select a range; therefore,<br />

the active cell doesn’t change when this macro is executed.<br />

Copying a variable-size range<br />

Often, you want to copy a range of cells in which the exact row and column dimensions are unknown.<br />

Figure 44.1 shows a range on a worksheet. This range contains data that is updated weekly. Therefore, the<br />

number of rows changes. Because the exact range address is unknown at any given time, writing a macro to<br />

copy the range can be challenging.<br />

758

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

Saved successfully!

Ooh no, something went wrong!