Converting Row to Column Format

cantab04

New Member
Joined
Jan 12, 2018
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to convert the following table:

ID NumberPurchase AmountPurchase Type
23$86Product X
24$56Product Y
25$66Product X
23$88Product Y
23$90Product Z

<tbody>
</tbody>


into this format:

ID NumberPurchase Amount 1Purchase Type 1Purchase Amount 2Purchase Type 2Purchase Amount 3Purchase Type 3
23$86Product X$88Product Y$90Product Z
24$56Product Y
25$66Product X

<tbody>
</tbody>


Essentially, I need for Excel to recognize instances in which a unique identifier (ID here) is represented on multiple rows, and then convert the data associated with these instances into a Column format.

I have KUTools and have a basic understanding of VBA. Any help is much appreciated!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
This assumes your initial table starts in A1 and rearranges the table starting in E1 of the same worksheet.
Code:
Sub RearrangeData()
'assumes initial data headers start in A1 of the active sheet
Dim Rtbl As Range, Rdata As Range, V As Variant, Cols As Long, i As Long, j As Long, nxCol As Long
Set Rtbl = Range("A1").CurrentRegion
Set Rdata = Rtbl.Offset(1, 0).Resize(Rtbl.Rows.Count - 1)
V = Rdata.Value
Application.ScreenUpdating = False
'how many columns needed
Cols = Application.Max(Application.CountIf(Rdata.Columns(1), Rdata.Columns(1)))
'rearrange data starting in E1
Range("E1").Resize(1, 2 * Cols + 1).EntireColumn.ClearContents
Rtbl.Columns(1).AdvancedFilter Action:=xlFilterCopy, copytorange:=[E1], unique:=True
j = 1
For i = 1 To 2 * Cols Step 2
    Range("E1").Offset(0, i) = "Purchase Amount " & j
    Range("E1").Offset(0, i + 1) = "Purchase Type " & j
    j = j + 1
Next i
Range("E1").CurrentRegion.EntireColumn.AutoFit
For Each c In Range("E2:E" & Cells(Rows.Count, "E").End(xlUp).Row)
    For i = LBound(V, 1) To UBound(V, 1)
        If V(i, 1) = c.Value Then
            nxCol = Cells(c.Row, Columns.Count).End(xlToLeft).Column + 1
            Cells(c.Row, nxCol) = V(i, 2)
            Cells(c.Row, nxCol + 1) = V(i, 3)
        End If
        Next i
        nxCol = 0
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,483
Messages
6,125,064
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top