代表某一單元格、某一行、某一列、某一選定區(qū)域(該選定區(qū)域可包含一個(gè)或若干連續(xù)單元格塊)。
本部分將對(duì)下列返回 Range 對(duì)象的屬性進(jìn)行說(shuō)明:
? | Range 屬性 |
? | Cells 屬性 |
可用 Range(arg)(其中 arg 為區(qū)域名稱)返回代表單個(gè)單元格或單元格區(qū)域的 Range 對(duì)象。下例將單元格 A1 中的值賦給單元格 A5。
myChart.Application.DataSheet.Range("A5").Value = _
myChart.Application.DataSheet.Range("A1").Value
下例將單元格區(qū)域 A1:H8 中所有單元格的值都設(shè)置為 20。
myChart.Application.DataSheet.Range("A1:H8").Value = 20
可用 Cells(row, column)(其中 row 為行號(hào),column 為列號(hào))返回單個(gè)單元格。下例將單元格 A1 賦值為 24(“A”列為數(shù)據(jù)表上的第二列,“1”行為數(shù)據(jù)表上的第二行)。
myChart.Application.DataSheet.Cells(2, 2).Value = 24
雖然也可用 Range("A1") 返回單元格 A1,但有時(shí)用 Cells 屬性更為方便,因?yàn)槭褂迷搶傩詴r(shí),可用變量指定行和列。下例在數(shù)據(jù)表上創(chuàng)建行列標(biāo)題。
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) 記號(hào)更為簡(jiǎn)便(而且也是更好的編程習(xí)慣)。
可用 expression.Cells(row, column) 返回單元格區(qū)域中的一部分,其中 expression 是返回 Range 對(duì)象的表達(dá)式,row 和 column 為相對(duì)于該區(qū)域左上角的偏移量。下例設(shè)置單元格 C5 的值。