Range
object) is assumed. This example also uses the second syntax of the Range
property. With this syntax, the first argument is the cell at the top left of the range, and the second argument is the cell at the lower right of the range.
The following example uses the Excel range intersection operator (a space) to return the intersection of two ranges. In this case, the intersection is a single cell, C6. Therefore, this statement enters 3
in cell C6:
Range("C1:C10 A6:E6") = 3
Finally, if the range you're referencing is a noncontiguous range (a range where not all the cells are adjacent to each other), you can use commas to serve as a union operator. For example, the following statement enters the value 4
in five cells that make up a noncontiguous range. Note that the commas are within the quote marks.
Range("A1,A3,A5,A7,A9") = 4
So far, all the examples have used the Range
property on a Worksheet
object. As mentioned, you can also use the Range
property on a Range
object. For example, the following line of code treats the Range
object as if it were the upper-left cell in the worksheet, and then it enters a value of 5
in the cell that would be B2. In other words, the reference returned is relative to the upper-left corner of the Range
object. Therefore, the statement that follows enters a value of 5
into the cell directly to the right and one row below the active cell:
ActiveCell.Range("B2") = 5
Fortunately, you can access a cell relative to a range in a much clearer way—the Offset
property. We discuss this property after the next section.
The Cells property
Another way to reference a range is to use the Cells
property. You can use the Cells
property, like the Range
property, on Worksheet
objects and Range
objects. Check the Help system, and you see that the Cells
property has three syntaxes.
object.Cells(rowIndex, columnIndex) object.Cells(rowIndex) object.Cells
Some examples demonstrate how to use the Cells
property. The first example enters the value 9
in cell A1 on Sheet1
. In this case, we're 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
in 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
in 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
NOTE
The real advantage of this type of cell referencing will be apparent when you explore variables and looping (in Chapter 3, “VBA Programming Fundamentals”). In most cases, you don't use actual values for the arguments; rather, you use variables.
To enter a value of 5
in 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
property 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 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 cell 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
To display the value in the last cell in a worksheet (XFD1048576), use this statement:
MsgBox ActiveSheet.Cells(17179869184)
You can also use this syntax with a Range
object. In this case, the cell returned is relative to the Range
object referenced. For example, if the Range
object is A1:D10 (40 cells), the Cells
property can have an argument from 1
to 40
and can return one of the cells in the Range
object. In the following example, a value of 2000
is entered in cell A2 because A2 is the 5th cell (counting from the top, to the right, and then down) in the referenced range:
Range("A1:D10").Cells(5) = 2000
NOTE
In the preceding example, the argument for the Cells
property isn't limited to values between 1
and 40
. If the argument exceeds the number of cells in the range, the counting continues as if the range were taller than it actually is. Therefore, a statement like the preceding one could change the value in a cell that's outside the range A1:D10. The statement that follows, for example, changes the value in cell A11:
Range("A1:D10").Cells(41) = 2000
The third syntax for the Cells
property simply returns all cells on the referenced worksheet. Unlike the other two syntaxes, in this one, the return data isn't a single cell. This example uses the ClearContents
method on the range returned by using the Cells
property on the active worksheet. The result is that the content of every cell on the worksheet is cleared.
ActiveSheet.Cells.ClearContents
Getting information from a cell
If you need to get the contents of a cell, VBA provides several properties. The following are the most commonly used properties:
The Formula property returns the formula in a single cell, if the cell has one. If the cell doesn't contain a formula, it returns the value in the cell. The Formula property is a read/write property. Variations on this property include FormulaR1C1, FormulaLocal, and FormulaArray. (Consult the Help system for details.)
The Value property returns the raw, unformatted value in the cell. This property is a read/write property.
The Text property returns the text that is displayed in the cell. If the cell contains a numeric value, this property includes all the formatting, such as commas and currency symbols. The Text property is a read-only property.
The Value2 property is just like the Value property, except that it doesn't use the Date and Currency data types. Rather, this property converts Date and Currency data types to Variants containing Doubles. If a cell contains the date 5/1/2019, the Value property returns it as a Date, while the Value2 property returns it as a double (for example, 43586).
The Offset property
The Offset
property, like the Range
and Cells
properties, also returns a Range
object. But unlike the other two methods discussed, the Offset
property applies only to a Range
object and no other class. Its syntax is as follows:
object.Offset(rowOffset, columnOffset)
The Offset
property