Range.Cells 屬性 (Excel)Range.Cells property (Excel)

可傳回 Range 物件,代表指定範圍內的儲存格。Returns a Range object that represents the cells in the specified range.

注意

對於開發跨多個平台,擴充使用者 Office 體驗的解決方案感到興趣嗎?Interested in developing solutions that extend the Office experience across multiple platforms? 請參閱新的 Office 增益集模型Check out the new Office Add-ins model. 與 VSTO 增益集和解決方案相比,Office 增益集的佔用空間較小,而且您可以使用幾乎任何網頁程式技術 (例如 HTML5、JavaScript、CSS3 和 XML) 來建立這些增益集。Office Add-ins have a small footprint compared to VSTO Add-ins and solutions, and you can build them by using almost any web programming technology, such as HTML5, JavaScript, CSS3, and XML.

語法Syntax

expression.Cellsexpression.Cells

expression 代表 Range 物件的變數。expression A variable that represents a Range object.

註解Remarks

傳回值是由單一儲存格組成的 Range,可讓您使用 Item 及其兩個參數版本,然後讓 For Each 迴圈逐一查看單一儲存格。The return value is a Range consisting of single cells, which allows to use the version of the Item with two parameters and lets For Each loops iterate over single cells.

因為 Range 的預設成員會將帶參數的呼叫轉傳到 Item 屬性,您可以緊接著在 Cells 關鍵字後面指定列索引及欄索引,而不是明確呼叫 ItemBecause the default member of Range forwards calls with parameters to the Item property, you can specify the row and column index immediately after the Cells keyword instead of an explicit call to Item.

在不指定物件辨識符號的情況下使用 Cells 即等於使用 ActiveSheet.CellsUsing Cells without an object qualifier is equivalent to ActiveSheet.Cells.

範例Example

此範例會在使用中活頁簿的 Sheet1 上,將儲存格 B2:D6 的字型樣式設定為斜體。This example sets the font style for cells A1:C5 on Sheet1 to italic.

With Worksheets("Sheet1").Range("B2:Z100") 
   .Range(.Cells(1, 1), .Cells(5, 3)).Font.Italic = True
End With

此範例將搜尋名為 myRange 的一欄資料。This example scans a column of data named myRange. 如果儲存格的值與其後儲存格的值相同,此範例將顯示這個包含重複資料的儲存格的位址。If a cell has the same value as the cell immediately preceding it, the example displays the address of the cell that contains the duplicate data.

Set r = Range("myRange") 
For n = 2 To r.Rows.Count 
    If r.Cells(n-1, 1) = r.Cells(n, 1) Then 
        MsgBox "Duplicate data in " & r.Cells(n, 1).Address 
    End If 
Next

此範例示範 Cells 如何變更 Item 成員的行為。This example demonstrates how Cells changes the behavior of the Item member.

Public Sub PrintRangeAdresses
   Dim columnsRange As Excel.Range
   Set columnsRange = ThisWorkBook.Worksheets("exampleSheet").Range("B2:Z100").Columns
   
   Debug.Print columnsRange.Item(2).Address         'Prints "$C$2:$C$100" 
   Debug.Print columnsRange.Cells.Item(2).Address   'Prints "$C$2" 
   Debug.Print columnsRange.Cells.Item(2,1).Address 'Prints "$B$3"   
End Sub

此範例示範 Cells 如何變更列舉行為。This example demonstrates how Cells changes the enumeration behavior.

Public Sub PrintAllRangeAdresses
   Dim columnsRange As Excel.Range
   Set columnsRange = ThisWorkBook.Worksheets("exampleSheet").Range("B2:C3").Columns
   
   Dim columnRange As Excel.Range
   For Each columnRange In columnsRange
      Debug.Print columnRange.Address   'Prints "$B$2:$B$3", "$C$2:$C$3"
   Next
   
   Dim cell As Excel.Range
   For Each cell In columnsRange.Cells
      Debug.Print cell.Address          'Prints "$B$2", "$C$2", "$B$3", "$C$3"
   Next  
End Sub

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.