Excel 单元格的引用方法 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    1-1 使用Cells属性

    使用Cells属性返回一个Range对象,如下面的代码所示,

Excel 单元格的引用方法

    00.00.#001 Sub Cell()

    00.#002     Dim icell As Integer

    00.#003     For icell = 1 To 100

    00.#004         Sheet2.Cells(icell, 1).Value = icell

    00.#005     Next

    00.#006 End Sub

    普通浏览复制代码保存代码打印代码

    01.#001 Sub Cell()

    02.#002     Dim icell As Integer

    03.#003     For icell = 1 To 100

    04.#004         Sheet2.Cells(icell, 1).Value = icell

    05.#005     Next

    06.#006 End Sub#001 Sub Cell()

    #002     Dim icell As Integer

    #003     For icell = 1 To 100

    #004         Sheet2.Cells(icell, 1).Value = icell

    #005     Next

    #006 End Sub

    代码解析:

    Cell过程使用For...Next语句为工作表中的A1:A100单元格区域填入序号,

电脑资料

Excel 单元格的引用方法》(https://www.unjs.com)。

    Cells属性指定单元格区域中的单元格,语法如下:

    Cells(RowIndex, ColumnIndex)

    参数RowIndex是可选的,表示引用区域中的行序号。

    参数ColumnIndex是可选的,表示引用区域中的列序号。

    如果缺省参数,Cells属性返回引用对象的所有单元格。

    Cells属性的参数可以使用变量,因此经常应用于在单元格区域中循环。

    1-2 使用快捷记号

    在VBA中可以将A1引用样式或命名区域名称使用方括号括起来,作为Range属性的快捷方式,这样就不必键入单词“Range”或使用引号,如下面的代码所示。

    00.00.#001 Sub Fastmark()

    00.#002     [A1:A5] = 2

    00.#003     [Fast] = 4

    00.#004 End Sub

    普通浏览复制代码保存代码打印代码

    01.#001 Sub Fastmark()

    02.#002     [A1:A5] = 2

    03.#003     [Fast] = 4

    04.#004 End Sub#001 Sub Fastmark()

    #002     [A1:A5] = 2

    #003     [Fast] = 4

    #004 End Sub

    代码解析:

    Fastmark过程使用快捷记号为单元格区域赋值。

    第2行代码使用快捷记号将活动工作表中的A1:A5单元格赋值为2。

    第3行代码将工作簿中已命名为“Fast”的单元格区域赋值为4。

    注意 使用快捷记号引用单元格区域时只能使用固定字符串而不能使用变量

最新文章