使用VBA

时间:2016-05-13 22:53:36

标签: excel vba excel-vba

我有一个包含3列和30行的表。我正在尝试编写一个VBA宏,它将第一行向右移动1个单元格,第二行向右移动2个单元格,依此类推。

例如,这个

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 个答案:

答案 0 :(得分: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 :(得分: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

修改#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