friday13th,
Welcome to the MrExcel forum.
What version of Excel and Windows are you using?
The macro will copy your raw data to an array.
Then, the macro will sort the data by column A ascending, column B ascending.
Then the results will be written to column E and F.
Then the data in column A and B will be replaced by the array.
Sample raw data:
Excel 2007
| A | B | C | D | E | F |
---|
Transaction ID | Product Name | | | | | |
milk | | | | | | |
bread | | | | | | |
jam | | | | | | |
bread | | | | | | |
honey | | | | | | |
| | | | | | |
<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]3[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]4[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]5[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]6[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]7[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
</tbody>
Sheet1
After the macro:
Excel 2007
| A | B | C | D | E | F |
---|
Transaction ID | Product Name | Transaction ID | Product Name | | | |
milk | bread, milk | | | | | |
bread | jam | | | | | |
jam | bread, honey | | | | | |
bread | | | | | | |
honey | | | | | | |
| | | | | | |
<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]1[/TD]
[TD="align: center"]3[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]
[TD="align: center"]4[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]3[/TD]
[TD="align: center"]5[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]6[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]7[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
</tbody>
Sheet1
Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).
1. Copy the below code
2. Open your NEW workbook
3. Press the keys
ALT +
F11 to open the Visual Basic Editor
4. Press the keys
ALT +
I to activate the Insert menu
5. Press
M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys
ALT +
Q to exit the Editor, and return to Excel
8. To run the macro from Excel press
ALT +
F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
Code:
Option Explicit
Sub ReorgData()
' hiker95, 11/16/2013
' http://www.mrexcel.com/forum/excel-questions/739798-data-transformation-help-transaction-data-currently-columns-needs-altered.html
Dim oa As Variant
Dim r As Long, lr As Long, n As Long, nr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
oa = Range("A2:B" & lr)
Range("A2:B" & lr).Sort key1:=Range("A2"), order1:=1, key2:=Range("B2"), order1:=1
Columns("E:F").ClearContents
Range("E1").Resize(, 2).Value = Range("A1").Resize(, 2).Value
For r = 2 To lr
n = Application.CountIf(Columns(1), Cells(r, 1).Value)
nr = Range("E" & Rows.Count).End(xlUp).Offset(1).Row
If n = 1 Then
Range("E" & nr).Resize(, 2).Value = Range("A" & r).Resize(, 2).Value
Else
Range("E" & nr).Value = Range("A" & r).Value
Range("F" & nr).Value = Join(Application.Transpose(Range("B" & r & ":B" & r + n - 1)), ", ")
End If
r = r + n - 1
Next r
Range("A2:B" & lr) = oa
Columns("E:F").AutoFit
End Sub
Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension
.xlsm
Then run the
ReorgData macro.