Редагувати

Поділитися через


Range.Rows property (Excel)

Returns a Range object that represents the rows in the specified range.

Syntax

expression.Rows

expression A variable that represents a Range object.

Remarks

To return a single row, use the Item property or equivalently include an index in parentheses. For example, both Selection.Rows(1) and Selection.Rows.Item(1) return the first row of the selection.

When applied to a Range object that is a multiple selection, this property returns rows from only the first area of the range. For example, if the Range object someRange has two areas—A1:B2 and C3:D4—,someRange.Rows.Count returns 2, not 4. To use this property on a range that may contain a multiple selection, test Areas.Count to determine whether the range is a multiple selection. If it is, loop over each area in the range, as shown in the third example.

The returned range might be outside the specified range. For example, Range("A1:B2").Rows(5) returns cells A5:B5. For more information, see the Item property.

Using the Rows property without an object qualifier is equivalent to using ActiveSheet.Rows. For more information, see the Worksheet.Rows property.

Example

This example deletes the range B4:Z4 on Sheet1 of the active workbook.

Worksheets("Sheet1").Range("B2:Z44").Rows(3).Delete

This example deletes rows in the current region on worksheet one of the active workbook where the value of cell one in the row is the same as the value of cell one in the previous row.

For Each rw In Worksheets(1).Cells(1, 1).CurrentRegion.Rows
   this = rw.Cells(1, 1).Value 
   If this = last Then rw.Delete 
   last = this 
Next

This example displays the number of rows in the selection on Sheet1. If more than one area is selected, the example loops through each area.

Public Sub ShowNumberOfRowsInSheet1Selection
   Worksheets("Sheet1").Activate 
   
   Dim selectedRange As Excel.Range
   Set selectedRange = Selection
   
   Dim areaCount As Long
   areaCount = Selection.Areas.Count 
   
   If areaCount <= 1 Then 
      MsgBox "The selection contains " & _ 
             Selection.Rows.Count & " rows." 
   Else 
      Dim areaIndex As Long
      areaIndex = 1 
      For Each area In Selection.Areas 
         MsgBox "Area " & areaIndex & " of the selection contains " & _ 
                area.Rows.Count & " rows." 
         areaIndex = areaIndex + 1 
      Next 
   End If
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.