Combining columns into row

jetwatch

New Member
Joined
Aug 10, 2011
Messages
5
Here is the data I have:

column 1 column 2 column 3
smith blue 4
smith green 3
smith red 4
jones blue 2
jones red 2
jones green 3
jones yellow 2
brown red 3
brown blue 3

etc.

I need it to be like this:

Column 1 column 2 column 3 column 4 column 5 column 6 etc.
smith blue 4 green 3 red 4
jones blue 2 red 2 green 3

Thank you for the assistance.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
if your data is A1:C9,

you put unique names in column F, and type in G1

=IFERROR(INDEX($B$1:$C$9,SMALL(IF($F1=$A$1:$A$9,ROW($B$1:$C$9)),INT((COLUMNS($G1:G1)-1)/2)+1),MOD(COLUMNS($G1:G1)-1,2)+1),"")

you enter the above with control shift enter, drag it to the right as far as your data could go and then drag down
 
Upvote 0
jetwatch,

Welcome to the MrExcel Forum.

How about a macro?

What version of Excel and Windows are you using?

Are you using a PC or a Mac?

Sample raw data:


Excel 2007
ABCDEFGHIJKLM
1smithblue4
2smithgreen3
3smithred4
4jonesblue2
5jonesred2
6jonesgreen3
7jonesyellow2
8brownred3
9brownblue3
10
Sheet1


After the macro:


Excel 2007
ABCDEFGHIJKLM
1smithblue4smithblue4green3red4
2smithgreen3jonesblue2red2green3yellow2
3smithred4brownred3blue3
4jonesblue2
5jonesred2
6jonesgreen3
7jonesyellow2
8brownred3
9brownblue3
10
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 ReorgDataSDPlus()
' hiker95, 08/22/2014, ME762830
Dim r As Long, lr As Long, n As Long, maxn As Long
Dim b As Variant, i As Long, ii As Long, t As Long
Dim rng As Range, c As Range, s, z
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 1 To lr
  n = Application.CountIf(Columns(1), Cells(r, 1).Value)
  If n > maxn Then maxn = n
  r = r + n - 1
Next r
Set rng = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
With CreateObject("Scripting.Dictionary")
  .CompareMode = vbTextCompare
  For Each c In rng
    If Not .Exists(c.Value) Then
      .Add c.Value, c.Offset(, 1) & "," & c.Offset(, 2)
    Else
      .Item(c.Value) = .Item(c.Value) & "," & c.Offset(, 1) & "," & c.Offset(, 2)
    End If
  Next
  z = Application.Transpose(Array(.Keys, .Items))
End With
ReDim b(1 To UBound(z, 1), 1 To (maxn * (maxn - 1) + 1))
For i = 1 To UBound(z, 1)
  ii = ii + 1
  b(ii, 1) = z(i, 1)
  If InStr(z(i, 2), ",") = 0 Then
    b(ii, 2) = z(i, 2)
  ElseIf InStr(z(i, 2), ",") > 0 Then
    s = Split(z(i, 2), ",")
    For t = LBound(s) To UBound(s)
      b(ii, t + 2) = s(t)
    Next t
  End If
Next i
Range("E1").Resize(UBound(b, 1), UBound(b, 2)) = b
Columns.AutoFit
Application.ScreenUpdating = True
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 ReorgDataSDPlus macro.
 
Upvote 0
jetwatch,

I see you just posted a link to your workbook/worksheets with your actual raw data, and, that the raw data should also contains titles in row 1.

Be back in a little while.
 
Upvote 0
jetwatch,

The below macro will adjust for a varying number of rows of raw data.

Sample raw data (not all rows and columns are shown for brevity):


Excel 2007
ABC
1NamesCourseGrade
2AyySarahSpanish 3 CP4
3AyySarahMath Analysis H2
4AyySarahAP Biology2
5AyySarahAP US History2
6AyySarahScientific Research4
7AyySarahAP English Lang/Comp4
8AyySarahPE Girls Tennis4
9BakDanielleSpanish 3 CP2
10BakDanielleUS History CP3
11BakDanielleMarine Science3
12BakDanielleFunctions Stats and Trig CP3
13BakDanielleEnglish 11 CP2
14BakDanielleSports / Entertainment Marketing4
15BakDaniellePE Girls Tennis4
16BelKellyUS Government and Politics CP4
17BelKellyAP Art History4
18BelKellyArt Media4
19BelKellyAP English Lit/Comp4
20BelKellyPsychology CP4
21BelKellyPE Girls Tennis4
22BruSkyeUS History CP Per 14
23BruSkyeAlgebra 2 CP3
24BruSkyeEnglish 11 CP4
25BruSkyePhotography 1-24
26BruSkyeChemistry CP4
27BruSkyeSpanish 3 CP4
28BruSkyePE Girls Tennis4
Sheet1



After the updated macro (not all rows and columns are shown for brevity):


Excel 2007
FGHIJKL
1NamesCourseGradeCourseGradeCourseGrade
2AyySarahSpanish 3 CP4Math Analysis H2AP Biology2
3BakDanielleSpanish 3 CP2US History CP3Marine Science3
4BelKellyUS Government and Politics CP4AP Art History4Art Media4
5BruSkyeUS History CP Per 14Algebra 2 CP3English 11 CP4
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).

Code:
Option Explicit
Sub ReorgDataSDPlusV2()
' hiker95, 03/08/2014, ME762830
Dim r As Long, lr As Long, n As Long, maxn As Long
Dim b As Variant, i As Long, ii As Long, t As Long
Dim rng As Range, c As Range, s, z
Dim lc As Long
Application.ScreenUpdating = False
Sheets("Sheet1").Activate
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
  n = Application.CountIf(Columns(1), Cells(r, 1).Value)
  If n > maxn Then maxn = n
  r = r + n - 1
Next r
Set rng = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
With CreateObject("Scripting.Dictionary")
  .CompareMode = vbTextCompare
  For Each c In rng
    If Not .Exists(c.Value) Then
      .Add c.Value, c.Offset(, 1) & "," & c.Offset(, 2)
    Else
      .Item(c.Value) = .Item(c.Value) & "," & c.Offset(, 1) & "," & c.Offset(, 2)
    End If
  Next
  z = Application.Transpose(Array(.Keys, .Items))
End With
ReDim b(1 To UBound(z, 1), 1 To (maxn * (maxn - 1) + 1))
For i = 1 To UBound(z, 1)
  ii = ii + 1
  b(ii, 1) = z(i, 1)
  If InStr(z(i, 2), ",") = 0 Then
    b(ii, 2) = z(i, 2)
  ElseIf InStr(z(i, 2), ",") > 0 Then
    s = Split(z(i, 2), ",")
    For t = LBound(s) To UBound(s)
      b(ii, t + 2) = s(t)
    Next t
  End If
Next i
Range("F1").Resize(, 3).Value = Range("A1").Resize(, 3).Value
Range("F2").Resize(UBound(b, 1), UBound(b, 2)) = b
lc = Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
For i = 9 To lc Step 2
  Cells(1, i).Resize(, 2).Value = Cells(1, 2).Resize(, 2).Value
Next i
Columns.AutoFit
Application.ScreenUpdating = True
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 ReorgDataSDPlusV2 macro.
 
Upvote 0
Heres another..

Code:
Private Sub CommandButton1_Click()
    Dim X0, it, Z, i As Long, LC As Long, ii
    Application.ScreenUpdating = False
    With CreateObject("scripting.dictionary")
        For Each it In Sheets("Sheet1").Columns(1).SpecialCells(2).Offset(1).Resize(Columns(1).SpecialCells(2).Count - 1)
            X0 = .Item(it.Value)
        Next
        Z = .keys
    End With
    With Sheets("Sheet1").Range("A1:C" & Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row)
        For i = LBound(Z) To UBound(Z)
            .AutoFilter 1, Z(i)
            Cells(i + 2, 5).Value = Z(i)
            For Each ii In .Offset(1, 1).Resize(.Rows.Count - 1, 2).SpecialCells(12)
                LC = Range("IV" & i + 2).End(xlToLeft).Column
                Cells(i + 2, LC).Offset(, 1).Value = ii.Value
            Next ii
        Next i
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub

<b>Sheet1</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:97px;" /><col style="width:97px;" /><col style="width:97px;" /><col style="width:97px;" /><col style="width:97px;" /><col style="width:97px;" /><col style="width:97px;" /><col style="width:64px;" /><col style="width:64px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td><td >H</td><td >I</td><td >J</td><td >K</td><td >L</td><td >M</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td >Name</td><td >Color</td><td >Value</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td >smith</td><td >blue</td><td style="text-align:right; ">4</td><td > </td><td >smith</td><td >blue</td><td style="text-align:right; ">4</td><td >green</td><td style="text-align:right; ">3</td><td >red</td><td style="text-align:right; ">4</td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td >smith</td><td >green</td><td style="text-align:right; ">3</td><td > </td><td >jones</td><td >blue</td><td style="text-align:right; ">2</td><td >red</td><td style="text-align:right; ">2</td><td >green</td><td style="text-align:right; ">3</td><td >yellow</td><td style="text-align:right; ">2</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td >smith</td><td >red</td><td style="text-align:right; ">4</td><td > </td><td >brown</td><td >red</td><td style="text-align:right; ">3</td><td >blue</td><td style="text-align:right; ">3</td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td >jones</td><td >blue</td><td style="text-align:right; ">2</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td >jones</td><td >red</td><td style="text-align:right; ">2</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td >jones</td><td >green</td><td style="text-align:right; ">3</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td >jones</td><td >yellow</td><td style="text-align:right; ">2</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td >brown</td><td >red</td><td style="text-align:right; ">3</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td >brown</td><td >blue</td><td style="text-align:right; ">3</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr></table> <br />Excel tables to the web - Excel Jeanie Html 4
 
Upvote 0
Hello Jet,

the formula for allowing for headings would be

=IFERROR(INDEX($B$2:$C$125,SMALL(IF($F2=$A$2:$A$125,ROW($B$2:$C$125)-ROW($B$2)+1),INT((COLUMNS($G1:G1)-1)/2)+1),MOD(COLUMNS($G1:G1)-1,2)+1),"")

with control shift enter.

I have also uploaded your file with the formula applied for your convenience

you can get it here: Zippyshare.com - my data copy.xlsx
 
  • Like
Reactions: apo
Upvote 0
just for interest ...

couple of codes.

first does as you ask by elementary VBA, mainly counting and copying, but also retaining the formats from the cells of the original columns

the second gives a different layout of the results - possibly one of interest to you

in each case, just run the code and the results should appear from E1 down and across
Code:
Sub a_code()
Dim u As Boolean, a, i&, r&, s&, c
a = Cells(1).CurrentRegion.Resize(, 3)

c = a(2, 1): r = 2: s = 5
For i = 3 To UBound(a)
    If a(i, 1) = c Then
        If u = 0 Then Cells(i - 1, 1).Resize(, 3).Copy Cells(r, 5): u = 1
        s = s + 2
        Cells(i, 2).Resize(, 2).Copy Cells(r, s + 1)
        If s > m Then m = s
    Else
        u = 0
        r = r + 1
        s = 5
        c = a(i, 1)
    End If
Next i
Cells(1).Resize(, 3).Copy Cells(5)
For i = 8 To m + 2 Step 2
    Cells(2).Resize(, 2).Copy Cells(i)
Next i
Columns(5).Resize(, m - 2).AutoFit
End Sub
Code:
Sub b_code()

Dim b1 As Object, b2 As Object
Dim c, a(), rws&, d&

Set b1 = CreateObject("scripting.dictionary")
Set b2 = CreateObject("scripting.dictionary")
b1.CompareMode = 1: b2.CompareMode = 1
rws = Cells(Rows.Count, 1).End(3).Row
c = Cells(1).Resize(rws, 3)
rws = UBound(c)
ReDim a(1 To rws, 1 To rws)
For d = 2 To rws
    If b1(c(d, 1)) = "" Then b1(c(d, 1)) = b1.Count + 1
    If b2(c(d, 2)) = "" Then b2(c(d, 2)) = b2.Count + 1
    a(b2(c(d, 2)), 1) = c(d, 2)
    a(1, b1(c(d, 1))) = c(d, 1)
    a(b2(c(d, 2)), b1(c(d, 1))) = a(b2(c(d, 2)), b1(c(d, 1))) + c(d, 3)
Next d

Range("E1").Resize(b2.Count + 1, b1.Count + 1) = a
Columns("e").Resize(, b1.Count + 1).AutoFit
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,980
Members
448,934
Latest member
audette89

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