Hello,
I am trying to convert a vertical list to a horizontal one, but I am having trouble placing the cells in the correct column.
Here's an example:
ID #| Code| Rate| Price
135 |A001| 2560|1300
135 |A003| 3260|9631
135 |A002| 4523|8523
This is the outcome I'm looking for:
ID #|Code1|Rate|Price|Code2|Rate|Price|Code3|Rate|Price
135 |A001|2560| 1300|A003|3260|9631|A002|4523|8523
However, I want to take it one more step and place all A001 cells in Column B, all A002 items in Column D, all A003 items in Column F, etc...
The code below will convert the data into a horizontal form. Any idea how to sort the data into the desired columns?
I would appreciate any help with this.
I am trying to convert a vertical list to a horizontal one, but I am having trouble placing the cells in the correct column.
Here's an example:
ID #| Code| Rate| Price
135 |A001| 2560|1300
135 |A003| 3260|9631
135 |A002| 4523|8523
This is the outcome I'm looking for:
ID #|Code1|Rate|Price|Code2|Rate|Price|Code3|Rate|Price
135 |A001|2560| 1300|A003|3260|9631|A002|4523|8523
However, I want to take it one more step and place all A001 cells in Column B, all A002 items in Column D, all A003 items in Column F, etc...
The code below will convert the data into a horizontal form. Any idea how to sort the data into the desired columns?
Code:
Option Explicit
Sub MergeData()
Dim LastRow As Long, NextCol As Long
Dim LastCol As Long, Rw As Long, Cnt As Long
Dim delRNG As Range
Application.ScreenUpdating = False
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1").CurrentRegion.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
Set delRNG = Range("A" & LastRow + 10)
For Rw = LastRow To 2 Step -1
If Cells(Rw, "A").Value = Cells(Rw - 1, "A").Value Then
Range(Cells(Rw, "B"), Cells(Rw, Columns.Count).End(xlToLeft)).Copy _
Cells(Rw - 1, Columns.Count).End(xlToLeft).Offset(0, 1)
Set delRNG = Union(delRNG, Range("A" & Rw))
End If
Next Rw
delRNG.EntireRow.Delete (xlShiftUp)
Set delRNG = Nothing
NextCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
LastCol = Cells(1, 1).CurrentRegion.Columns.Count
Range("B1", Cells(1, NextCol - 1)).Copy Range(Cells(1, NextCol), Cells(1, LastCol))
Cells.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
I would appreciate any help with this.