excel中列值的循环移位

时间:2014-11-20 08:56:32

标签: excel vba excel-vba

如何在Excel中执行列值的循环移位?

我试过了:

Sub RowInsert()
Application.ScreenUpdating = False
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.Cut
Range("A1").Select
ActiveSheet.Paste
Application.ScreenUpdating = True

End Sub

但它不起作用..有没有办法在没有VB的情况下做到这一点?

1 个答案:

答案 0 :(得分:1)

要将阵列就地循环,您可以改为执行此操作。它比插入线条,剪切和粘贴更有效,而且不会弄乱你的其余部分。

Dim arr As Variant
Dim temp As Variant
Dim r As Range
Dim i As Long
Dim n As Long

'Specify range
Set r = Range(Range("A1"), Range("A1").End(xlDown))
'Read range into a Variant array
arr = r.Value
'Count elements
n = UBound(arr, 1)
'Take bottom element and set aside
temp = arr(n, 1)
'Move each element down by 1, starting from bottom
For i = UBound(arr, 1) To 2 Step -1
    arr(i, 1) = arr(i - 1, 1)
Next i
'Put old bottom element in first place
arr(1, 1) = temp
'Write array back to sheet
r.Value = arr
相关问题