What does the Excel range.Rows property really do?

前端 未结 9 1834
故里飘歌
故里飘歌 2021-01-30 16:21

OK, I am finishing up an add-on project for a legacy Excel-VBA application, and I have once again run up against the conundrum of the mysterious range.Rows (?) and

9条回答
  •  一整个雨季
    2021-01-30 16:26

    Your two examples are the only things I have ever used the Rows and Columns properties for, but in theory you could do anything with them that can be done with a Range object.

    The return type of those properties is itself a Range, so you can do things like:

    Dim myRange as Range
    Set myRange = Sheet1.Range(Cells(2,2),Cells(8,8))
    myRange.Rows(3).Select
    

    Which will select the third row in myRange (Cells B4:H4 in Sheet1).

    update: To do what you want to do, you could use:

    Dim interestingRows as Range
    Set interestingRows = Sheet1.Range(startRow & ":" & endRow)
    

    update #2: Or, to get a subset of rows from within a another range:

    Dim someRange As Range
    Dim interestingRows As Range
    
    Set myRange = Sheet1.Range(Cells(2, 2), Cells(8, 8))
    
    startRow = 3
    endRow = 6
    
    Set interestingRows = Range(myRange.Rows(startRow), myRange.Rows(endRow))
    

提交回复
热议问题