创建“.Offset函数的更改版本”(或“如果在VBA中编写,会出现什么样的.Offset函数?”)

时间:2017-05-10 23:18:32

标签: excel vba excel-vba

我正在尝试编写VBA的偏移函数的改变变体,当一个单元偏移量通常落在指定范围之外时,取而代之的是超出该范围的单元数量。从范围的开始再次“包裹”它们。

我已经在网上搜索了一个人,他写了一个例子,如果它是用VBA代码写的那么.Offset函数会是什么样子,但是没有人这样做过。

有人会介意在.Offset函数在VBA代码中看起来像什么?

顺便提一下,我所做的改动类型是这样的:

Public Function MobOffset(off As Integer, REGCOL As Integer)


'The code below is to be used with Range(Cells.Rows(18), Cells.Rows(29))

Rem insert .offset code here:

If off > 29 Then
res = off - 29
off = res - 1 + 18
End If

If off < 18 Then
res = 18 Mod off
res2 = res - 1
off = 29 - res2
End If


End Function

1 个答案:

答案 0 :(得分:0)

通用公式

count = upper - lower + 1

index = (index - lower) % count + lower

所以(Mod需要调整负数):

off = (off - 18) Mod 12 + 18

If off < 18 Then off = off + 12
相关问题