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.

118<br />

Part III: Programming Concepts<br />

The Cells property takes two arguments: row and column. Both of these arguments<br />

are numbers, even though we usually refer to columns by using letters.<br />

For example, the following expression refers to cell C2 on Sheet2:<br />

Worksheets(“Sheet2”).Cells(2, 3)<br />

You can also use the Cells property to refer to a multi-cell range. The following<br />

example demonstrates the syntax you use:<br />

Range(Cells(1, 1), Cells(10, 10))<br />

This expression refers to a 100-cell range that extends from cell A1 (row 1,<br />

column 1) to cell J10 (row 10, column 10).<br />

The following statements both produce the same result; they enter a value of<br />

99 into a 10-by-10 range of cells. More specifically, these statements set the<br />

Value property of the Range object:<br />

Range(“A1:J10”).Value = 99<br />

Range(Cells(1, 1), Cells(10, 10)).Value = 99<br />

The advantage of using the Cells method to refer to ranges becomes apparent<br />

when you use variables rather than actual numbers as the Cells arguments.<br />

And things really start to click when you understand looping, which I cover in<br />

Chapter 10.<br />

The Offset property<br />

The Offset property provides another handy means for referring to ranges.<br />

This property, which operates on a Range object and returns another Range<br />

object, lets you refer to a cell that is a particular number of rows and columns<br />

away from another cell.<br />

Like the Cells property, the Offset property takes two arguments. The first<br />

argument represents the number of rows to offset; the second represents the<br />

number of columns to offset.<br />

The following expression refers to a cell one row below cell A1 and two columns<br />

to the right of cell A1. In other words, this refers to the cell commonly<br />

known as C2:<br />

Range(“A1”).Offset(1, 2)

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

Saved successfully!

Ooh no, something went wrong!