Adding an Offset Row to a Given Range. Excel VBA

无人久伴 提交于 2019-12-01 00:31:19

You have myRows as a Variant data type. You need to declare it as a Range object.

Dim myRows as Range

Then you would need to use the Set keyword to assign a Range object).

Set myRows = Range("1:10")

Then, use the range .Resize method:

Set myRows = myRows.Resize(myRows.Rows.Count+1, myRows.Columns.Count)

Otherwise, if you need to maintain myRows as type Variant, let me know and I can re-work this.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!