VisualBasic transpose macro

Assumption: We have list of values in excel sheet1
row1: A1
row2: A2
row3: A3
row4: A4
row5: B1
row6: B2
row7: B3
row8: B4

Task: We want to transpose this data in excel sheet2 into form.

row1: A1A2A3A4
row2: B1B2B3B4

So every four cells in four rows is transposed to one row with four columns

It could be done using following Visual Basic Macro:
<code>

  1. Sub Macro()
  2.     RowIndex = 1
  3.     Do
  4.     Sheets(“Sheet1″).Select
  5.     With Sheets(“Sheet1″)
  6.         Range(.Cells(RowIndex, 1), .Cells(RowIndex + 3, 1)).Select
  7.     End With 
  8.     Selection.Copy
  9.     Sheets(“Sheet2″).Select
  10.     RowIndexPaste = Round(RowIndex / 4) + 1
  11.     With Sheets(“Sheet2″)
  12.         Range(.Cells(RowIndexPaste, 1), .Cells(RowIndexPaste, 1)).Select
  13.     End With
  14.     Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
  15.         False, Transpose:=True
  16.     RowIndex = RowIndex + 4
  17.     Loop While Sheets(“Sheet1″).Cells(RowIndex, 1).Value <> “”
  18. End Sub

</code>

Macro check until check empty line.

If you know easiest way to resolve this task, let me know!

Napisz komentarz