Organizing Raw Data

crismat09

New Member
Joined
Mar 18, 2009
Messages
12
Hi All,

I need to organize some data coming from a raw spreadsheet into a separated table as displayed below.

[TABLE="width: 341"]
<colgroup><col span="2"><col><col span="3"></colgroup><tbody>[TR]
[TD]Raw Data[/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"]Rearranged Table[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD][/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Green[/TD]
[TD][/TD]
[TD]Green[/TD]
[TD]Green[/TD]
[TD]Blue[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Red[/TD]
[TD][/TD]
[TD]Red[/TD]
[TD]Orange[/TD]
[TD]Green[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Blue[/TD]
[TD][/TD]
[TD]Blue[/TD]
[TD]Red[/TD]
[TD]Red[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Green[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Orange[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Red[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Blue[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Green[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Red[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

RAW DATA: Column A represents countries and column B shows the products sold to each country.

I tried to use VLOOKUP and INDEX/MATCH, but both do not produce the expected results. Also tried using PivotTable, but no satisfactory results as well.

Any assistance is much appreciated.

Cris
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
crismat09,

Here is a macro solution for you to consider.

With your raw data in range A1:B9, the results will be written to range D1:F9.

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:
Sub ReorganizeData()
' hiker95, 11/29/2017, ME1033369
' Thank you: MickG , 2 / 2 / 2011, MG02Feb42
Dim rng As Range, r As Range
Dim o As Variant, omax As Integer, n As Long, lc As Long
Set rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
ReDim Out(1 To rng.Count, 1 To Columns.Count)
With CreateObject("Scripting.Dictionary")
  .CompareMode = vbTextCompare
  For Each r In rng
    If Not .Exists(r.Value) Then
      n = n + 1
      .Add r.Value, Array(n, 2)
      Out(n, 1) = r.Value: Out(n, 2) = r.Offset(, 1)
    Else
      o = .Item(r.Value)
      o(1) = o(1) + 1
      Out(o(0), o(1)) = r.Offset(, 1)
      .Item(r.Value) = o
      omax = Application.Max(o(1), omax)
    End If
  Next
  Range("D1").Resize(omax, .Count) = Application.Transpose(Out)
  Columns.AutoFit
End With
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, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorganizeData macro.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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