Microsoft Office Tutorials and References
In Depth Information
Working with Range Objects
Some examples demonstrate how to use the Cells property. The first example enters the value
9 into cell A1 on Sheet1 . In this case, I’m using the first syntax, which accepts the index number
of the row (from 1 to 1048576) and the index number of the column (from 1 to 16384):
Worksheets(“Sheet1”).Cells(1, 1) = 9
Here’s an example that enters the value 7 into cell D3 (that is, row 3, column 4) in the active
worksheet:
ActiveSheet.Cells(3, 4) = 7
You can also use the Cells property on a Range object. When you do so, the Range object
returned by the Cells property is relative to the upper-left cell of the referenced Range .
Confusing? Probably. An example may help clear up any confusion. The following instruction
enters the value 5 into the active cell. Remember, in this case, the active cell is treated as if it
were cell A1 in the worksheet:
ActiveCell.Cells(1, 1) = 5
The real advantage of this type of cell referencing will be apparent when I discuss
variables and looping (see Chapter 8). In most cases, you don’t use actual values for the
arguments; rather, you use variables.
To enter a value of 5 into the cell directly below the active cell, you can use the following
instruction:
ActiveCell.Cells(2, 1) = 5
Think of the preceding example as though it said this: “Start with the active cell and consider this
cell as cell A1. Place 5 in the cell in the second row and the first column.”
The second syntax of the Cells method uses a single argument that can range from 1 to
17,179,869,184. This number is equal to the number of cells in an Excel 2010 worksheet. The cells
are numbered starting from A1 and continuing right and then down to the next row. The 16,384th
cell is XFD1; the 16,385th is A2.
The next example enters the value 2 into cell SZ1 (which is the 520th cell in the worksheet) of the
active worksheet:
ActiveSheet.Cells(520) = 2
 
Search JabSto ::




Custom Search