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.

VBA Examples 44<br />

The following example demonstrates how to loop through all the cells in a range. In this case, the range is<br />

the current selection. In this example, Cell is a variable name that refers to the cell being processed.<br />

(Notice that this variable is declared as a Range object.) Within the For-Next loop, the single statement<br />

evaluates the cell. If the cell is negative, it’s converted to a positive value.<br />

Sub ProcessCells()<br />

Dim Cell As Range<br />

For Each Cell In Selection<br />

If Cell.Value < 0 Then Cell.Value = Cell.Value * -1<br />

Next Cell<br />

End Sub<br />

The preceding example works, but what if the selection consists of an entire column or an entire range?<br />

This is not uncommon because Excel lets you perform operations on entire columns or rows. But in this<br />

case, the macro seems to take forever because it loops through each cell — even those that are blank. What’s<br />

needed is a way to process only the nonblank cells.<br />

You can accomplish this task by using the SelectSpecial method. In the following example, the<br />

SelectSpecial method is used to create a new object: the subset of the selection that consists of cells<br />

with constants (as opposed to formulas). This subset is processed, with the net effect of skipping all blank<br />

cells and all formula cells.<br />

Sub ProcessCells2()<br />

Dim ConstantCells As Range<br />

Dim Cell As Range<br />

‘ Ignore errors<br />

On Error Resume Next<br />

‘ Process the constants<br />

Set ConstantCells = Selection.SpecialCells(xlConstants, xlNumbers)<br />

For Each Cell In ConstantCells<br />

If Cell.Value < 0 Then Cell.Value = Cell.Value * -1<br />

Next Cell<br />

End Sub<br />

The ProcessCells2 procedure works fast, regardless of what is selected. For example, you can select the<br />

range, select all columns in the range, select all rows in the range, or even select the entire worksheet. In all<br />

these cases, only the cells that contain constants are processed inside of the loop. This procedure is a vast<br />

improvement over the ProcessCells procedure presented earlier in this section.<br />

Notice that the following statement is used in the procedure:<br />

On Error Resume Next<br />

This statement causes Excel to ignore any errors that occur and simply to process the next statement. This<br />

statement is necessary because the SpecialCells method produces an error if no cells qualify and<br />

because the numerical comparison will fail if a cell contains an error value. Normal error checking is<br />

resumed when the procedure ends. To tell Excel explicitly to return to normal error-checking mode, use the<br />

following statement:<br />

On Error GoTo 0<br />

761

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

Saved successfully!

Ooh no, something went wrong!