通过添加和删除行和列来调整 VBA 中的范围对象的方法



在我的Windows Excel应用程序中,我经常需要通过添加和删除行和列来调整范围对象。我找不到可以简化此常见任务的方法。我不是在尝试更改数据;我正在尝试调整范围对象。如下所示:

Dim r As Range, s As Range
Set r = Range("B2:C3")     ' r is a 4-cell range object at B2
Set s = r.AdjustMe(OffsetRows:=-1,OffsetColumns:=2,Columns:=+2,Rows:=+3) ' s is now a 20-cell range at D1
Set s = s.AdjustMe(Rows:=-4) ' s is now a single row reference

我认为你只需要这样的东西:

dim r as long, rp as long, c as long, cp as long, rng as range
r = 1  'initial row  
c = 1  'initial column
rp = 2  'rows plus, the offset you want to work with
cp = 2  'colums plus, the offset you want to work with
set rng = cells(r,c).resize(rp,cp) 

最新更新