Читать книгу Excel 2019 Power Programming with VBA - Michael Alexander, Dick Kusleika - Страница 115
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