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. Office 增益集有較小型的耗用量相較於 VSTO 增益集與解決方案,而且您可以使用幾乎任何的 web 程式設計技術,例如 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.



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


由於 Item 屬性是 Range 物件的預設屬性,因此可以在 Cells 關鍵字後面緊接著指定列索引及欄索引。Because the Item property is the default property for the Range object, you can specify the row and column index immediately after the Cells keyword. 如需詳細資訊,請參閱 Item 屬性及本主題的範例。For more information, see the Item property and the examples for this topic.

使用此屬性時,如果不指定物件辨識符號,則會傳回代表現用工作表中所有儲存格的 Range 物件。Using this property without an object qualifier returns a Range object that represents all the cells on the active worksheet.


這個範例會將 Sheet1 上 A1:C5 儲存格的字型樣式設定為斜體。This example sets the font style for cells A1:C5 on Sheet1 to italic.

Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True

此範例將搜尋名為 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 n

此範例會查看欄位 C,針對具有註解的每個儲存格,它會將註解文字放入欄位 D 中,並且從欄位 C 刪除註解。This example looks through column C, and for every cell that has a comment, it puts the comment text into column D and deletes the comment from column C.

Sub SplitComments()
   'Set up your variables
   Dim cmt As Comment
   Dim iRow As Integer
   'Go through all the cells in Column C, and check to see if the cell has a comment.
   For iRow = 1 To WorksheetFunction.CountA(Columns(3))
      Set cmt = Cells(iRow, 3).Comment
      If Not cmt Is Nothing Then
         'If there is a comment, paste the comment text into column D and delete the original comment.
         Cells(iRow, 4) = Cells(iRow, 3).Comment.Text
         Cells(iRow, 3).Comment.Delete
      End If
   Next iRow
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.