Range 對象

此頁沒有內(nèi)容條目
內(nèi)容

DataSheet

parchildRange

代表某一單元格、某一行、某一列、某一選定區(qū)域(該選定區(qū)域可包含一個或若干連續(xù)單元格塊)。

使用 Range 對象

本部分將對下列返回 Range 對象的屬性進行說明:

?Range 屬性
?Cells 屬性

Range 屬性

可用 Range(arg)(其中 arg 為區(qū)域名稱)返回代表單個單元格或單元格區(qū)域的 Range 對象。下例將單元格 A1 中的值賦給單元格 A5。

myChart.Application.DataSheet.Range("A5").Value = _

    myChart.Application.DataSheet.Range("A1").Value

下例將單元格區(qū)域 A1:H8 中所有單元格的值都設置為 20。

myChart.Application.DataSheet.Range("A1:H8").Value = 20

Cells 屬性

可用 Cells(row, column)(其中 row 為行號,column 為列號)返回單個單元格。下例將單元格 A1 賦值為 24(“A”列為數(shù)據(jù)表上的第二列,“1”行為數(shù)據(jù)表上的第二行)。

myChart.Application.DataSheet.Cells(2, 2).Value = 24

雖然也可用 Range("A1") 返回單元格 A1,但有時用 Cells 屬性更為方便,因為使用該屬性時,可用變量指定行和列。下例在數(shù)據(jù)表上創(chuàng)建行列標題。

Sub SetUpTable()

With myChart.Application.DataSheet

    For theYear = 1 To 5

        .Cells(1, theYear + 1).Value = 1990 + theYear

    Next theYear

    For theQuarter = 1 To 4

        .Cells(theQuarter + 1, 1).Value = "Q" & theQuarter

    Next theQuarter

End With

End Sub

雖然可用 Visual Basic 字符串函數(shù)轉(zhuǎn)換 A1 樣式的引用,但使用 Cells(1, 1) 記號更為簡便(而且也是更好的編程習慣)。

可用 expression.Cells(row, column) 返回單元格區(qū)域中的一部分,其中 expression 是返回 Range 對象的表達式,rowcolumn 為相對于該區(qū)域左上角的偏移量。下例設置單元格 C5 的值。

myChart.Application.Range("C5:C10").Cells(1, 1).Value = 35