2016-05-13 25 views
1

我有一张3列30行的表格。我正在尝试编写一个VBA宏,它将第一行右移1格,第二行2格右移等等。用VBA以不同的给定范围移动每一行

例如,这

AAA 
BBB 
CCC 

应该是这样的:

AAA 
    BBB 
    CCC 

我试过到目前为止,只能将整个选择的范围:

Sub Macro() 
Set Table = Application.InputBox("Select table", Title:="Table", Type:=8) 
Table.Cut Table.Offset(0, 1) 
End Sub 

回答

2

这应该做你想要什么:

Sub Macro() 

Set Table = Application.InputBox("Select table", Title:="Table", Type:=8) 
Set Dest = Application.InputBox("Select destination", Title:="Destination", Type:=8) 

For i = 1 To Table.Rows.Count 
    For j = 1 To Table.Columns.Count 
     Table.Cells(i, j).Copy Dest.Cells(i + j - 1, i + 1) 
    Next j 
Next i 
End Sub 
1

之前:

enter image description here

的代码:

Sub asdfg() 
    Dim N As Long, i As Long 

    N = Cells(Rows.Count, "A").End(xlUp).Row 
    For i = 1 To N 
     Cells(i, 1).Cut (Cells(i, i + 1)) 
    Next i 
End Sub 

和后:

enter image description here

EDIT#1:

要偏移整个行使用:

Sub zxcvb() 
    Dim N As Long, i As Long 

    N = Cells(Rows.Count, "A").End(xlUp).Row 
    For i = 1 To N 
     Range(Cells(i, 1), Cells(i, i)).Insert Shift:=xlToRight 
    Next i 
End Sub 

前:

enter image description here

后:

enter image description here

+0

谢谢,但它不工作多列 – Lanza

+0

@Lanza你想让整行转移吗? –

+0

我想将选区的第一行向右移1格,第二行2格向右移等等。 – Lanza