我是Excel VBA的新手。我想编写一个函数,使当前向量(用户选择的范围)中的像元偏移用户指定的数量。
必须将单元格向上移动“ n”,然后必须在其余单元格向上移动以代替向上移动和移出单元格的位置后,将其显示在同一阵列的底部。数组。
任何建议都将不胜感激,我编写的当前代码无法正常工作,我所知甚少。
非常感谢!
LabelEncoder
答案 0 :(得分:1)
类似的事情应该起作用:
Sub Tester()
ShiftUp Range("B4:C13"), 3
End Sub
Sub ShiftUp(rng As Range, numRows As Long)
Dim tmp
With rng
tmp = .Rows(1).Resize(numRows).Value
.Rows(1).Resize(.Rows.Count - numRows).Value = _
.Rows(numRows + 1).Resize(.Rows.Count - numRows).Value
.Rows((.Rows.Count - numRows) + 1).Resize(numRows).Value = tmp
End With
End Sub
作为UDF:
Function ShiftUp(rng As Range, numRows As Long)
Dim d, dOut, r As Long, c As Long, rMod As Long, rTot As Long
Dim break As Long
d = rng.Value
dOut = rng.Value 'as a shortcut to creating an empty array....
rTot = UBound(d, 1)
break = rTot - numRows
For r = 1 To rTot
For c = 1 To UBound(d, 2)
'figure out which input row to use...
rMod = IIf(r <= break, r + numRows, -(break - r))
dOut(r, c) = d(rMod, c)
Next c
Next r
ShiftUp = dOut
End Function
请注意,这是一个数组公式,因此您需要选择一个与输入范围大小相同的范围,然后使用 Ctrl Shift Enter输入公式