Adding an Offset Row to a Given Range. Excel VBA

前端 未结 2 861
傲寒
傲寒 2021-01-07 00:10

I have a variable which at the beginning is set to a given range.

I want to have a loop statement that would take the next row down from the end of the given range a

2条回答
  •  一向
    一向 (楼主)
    2021-01-07 00:45

    You may also use

    Set myRows = Union(myRows, myRows.Offset(1, 0))
    

    EDIT:

    You asked for it!

    To remove one row from generic range

    Set myRows = RemoveRowFromRange(myRows, 10)
    
    ...
    
    Function RemoveRowFromRange(ByVal Range As Range, row_number As Long) As Range
        With Range.Worksheet
            Select Case row_number
            Case 1
                Set Range = Intersect(Range, .Range(.Rows(2), .Rows(.Rows.Count)))
            Case .Rows.Count
                Set Range = Intersect(Range, .Range(.Rows(1), .Rows(.Rows.Count - 1)))
            Case Else
                Set Range = Union(Intersect(Range, .Range(.Rows(1), .Rows(row_number - 1))), Intersect(Range, .Range(.Rows(row_number + 1), .Rows(.Rows.Count))))
            End Select
        End With
        Set RemoveRowFromRange = Range
    End Function
    

提交回复
热议问题