Reputation: 607
I have a table with 3 columns and 30 rows. I am trying to write a VBA macro that shifts first row with 1 cell to the right, the second row 2 cells to the right and so on.
For example, this
AAA
BBB
CCC
should look like this:
AAA
BBB
CCC
What I tried so far, could only move the whole selected range:
Sub Macro()
Set Table = Application.InputBox("Select table", Title:="Table", Type:=8)
Table.Cut Table.Offset(0, 1)
End Sub
Upvotes: 1
Views: 142
Reputation: 96753
Before:
The code:
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
and after:
EDIT#1:
To shift the entire row use:
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
Before:
and after:
Upvotes: 1
Reputation: 429
This should be doing what you want:
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
Upvotes: 2