+ Reply to Thread
Results 1 to 16 of 16

Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

Hybrid View

  1. #1
    Registered User
    Join Date
    09-12-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    17

    Question Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Hi

    I have a 2-column spreadsheet, where the values in column 1 repeat but the values in column 2 are unique.

    Column 1 Column 2
    ADD Joe
    ADD Pete
    ADD Harry
    ADD Tom
    ADD Jenny
    BEN Joe
    BEN Pete
    BEN Tom
    BEN Jenny
    CAD Joe
    CAD Pete
    CAD Tom
    CAD Harry

    I want to flip it so Column 1 becomes the Column headings for as many as needed (no repeats) and then the values of COlumn 2 fall in line where necessary.

    ADD BEN CAD
    Joe Joe Joe
    Pete Pete Pete
    Harry Harry
    Tom Tom Tom
    Jenny Jenny


    I tried PIVOT TABLES, but only get this:

    ADD BEN CAD
    Joe
    Pete
    Harry
    Tom
    Jenny

    (no matter how many times I edit this post, this last table doesn't appear correctly. The names are showing up as column 1 and ADD, BEN and CAD are showing as the headers for Columns 2 through 4. I hope this makes sense.)

    The TRANSPOSE function didn't work either.

    There are over 3,700 rows for this spreadsheet and can't even imagine attempting this manually! :-(

    I'd really appreciate any help on this.

    Thanks!
    Jim
    Last edited by jdegeorge; 09-12-2012 at 02:17 PM. Reason: Example tables didn't display correctly.

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Try

    Sub tranpose()
    Dim cell As Range
    Dim ValUnique As New Collection
    Dim i As Integer
    Dim LR As Long
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    
    On Error Resume Next
    For Each cell In Range("A1:A" & LR)
        ValUnique.Add cell.Value, CStr(cell.Value)
    Next cell
    
    For i = 1 To ValUnique.Count
        Cells(1, i + 3) = ValUnique.Item(i)
    Next i
    
    Range("B1:B" & LR).Copy Range("D2:F2")
    End Sub

  3. #3
    Registered User
    Join Date
    09-12-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    JieJenn

    Thanks for the quick response, but I haven't a clue what to do with your code. Basically, if Excel doesn't have a built-in function button I'm at a loss.

    Jim

  4. #4
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    A formula approach
    Step 1: Copy Col A to E1, Copy Col B to F2
    Step 2: Data tab> Remove Duplicates on E and then on F
    Copy Unique values from E to G1:?1 using Paste Special > transpose
    Step 3: In G2, dragged across and down, =IF(COUNTIFS($A$1:$A$13,G$1,$B$1:$B$13,$F2)>0,$F2,"")
    Step 4: Copy, Paste Special Values over new table to remove formulas
    See attachment
    Attached Files Attached Files
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  5. #5
    Registered User
    Join Date
    09-12-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Quote Originally Posted by ChemistB View Post
    A formula approach
    Step 1: Copy Col A to E1, Copy Col B to F2
    Step 2: Data tab> Remove Duplicates on E and then on F
    Copy Unique values from E to G1:?1 using Paste Special > transpose
    Step 3: In G2, dragged across and down, =IF(COUNTIFS($A$1:$A$13,G$1,$B$1:$B$13,$F2)>0,$F2,"")
    Step 4: Copy, Paste Special Values over new table to remove formulas
    See attachment

    I appreciate everyone's help, but I ran with ChemistB's solution because it looked more familiar. And...it worked! Thanks a bunch!

  6. #6
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Nice code approach Jie

  7. #7
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Quote Originally Posted by ChemistB View Post
    Nice code approach Jie
    More like I am too lazy to come up with a formula

  8. #8
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Oh my bad. Hit Alt + F11 to open the VBA window, on the menu on top, click on insert > module. Then just copy and paste the code and hit F5 (or click on Macro and select the procedure and hit run) to run the macro.

  9. #9
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    I spoke too soon. I get the following when I run your code
    Formula: copy to clipboard
    ADD	BEN	CAD
    Joe Joe Joe
    Pete Pete Pete
    Harry Harry Harry
    Tom Tom Tom
    Jenny Jenny Jenny
    Joe Joe Joe
    Pete Pete Pete
    Tom Tom Tom
    Jenny Jenny Jenny
    Joe Joe Joe
    Pete Pete Pete
    Tom Tom Tom
    Harry Harry Harry
    Somethings amiss.

  10. #10
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Good catch. Just came back from lunch. Here try the revised code

    Sub tranpose()
    Dim cell As Range
    Dim ValUnique As New Collection
    Dim i As Integer
    Dim LR As Long
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    On Error Resume Next
    For Each cell In Range("A1:A" & LR)
        ValUnique.Add cell.Value, CStr(cell.Value)
    Next cell
    
    For i = 1 To ValUnique.Count
        Cells(1, i + 3) = ValUnique.Item(i)
    Next i
    
    Rows("1").Insert
    Range("A1:B1") = Array("X", "Y")
    Range("B1:B" & LR + 1).AdvancedFilter xlFilterCopy, , Range("D3"), True
    Range("D3").Delete
    Range("D3:D" & Cells(Rows.Count, 4).End(xlUp).Row).Copy Range("E3:F3")
    Rows("1").Delete
    End Sub
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    09-12-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Thanks, but your attached file puts each person in each column D through F, when that's not what's represented in columns A and B.

  12. #12
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Maybe I misunderstand your question. Can you upload a sample file with what the final result should look like.

  13. #13
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Give this a try

    Sub abc()
     Dim i As Long
     Dim a, s
     
        a = Range("a1:b" & Cells(Rows.Count, 1).End(xlUp).Row)
        With CreateObject("Scripting.Dictionary")
           .CompareMode = 1
           For i = 1 To UBound(a)
               If Not .Exists(a(i, 1)) Then
                   .Item(a(i, 1)) = a(i, 1) & "," & a(i, 2)
               Else
                   .Item(a(i, 1)) = .Item(a(i, 1)) & "," & a(i, 2)
               End If
           Next
           a = .Items
           For i = LBound(a) To UBound(a)
               s = Split(a(i), ",")
               Cells(1, 4 + i).Resize(UBound(s) + 1) = WorksheetFunction.Transpose(s)
           Next
        End With
     
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  14. #14
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Heres as sample workbook

    Transpose.xlsm

  15. #15
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    Nope, Harry shouldn't appear under BEN, and Jenny shouldn't appear under CAD. Unless my formulas are wrong

  16. #16
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Convert 2-Column Spreadsheet to Multi-Columns based on 1 Column

    @JieJenn

    Heres an update to your code that should work

    Sub tranpose()
    Dim cell As Range
    Dim ValUnique As New Collection
    Dim i As Integer
    Dim LR As Long
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    On Error Resume Next
    For Each cell In Range("A1:A" & LR)
        ValUnique.Add cell.Value, CStr(cell.Value)
    Next cell
    
    Rows("1").Insert
    Range("A1:B1") = Array("X", "Y")
    For i = 1 To ValUnique.Count
        Cells(1, i + 3) = ValUnique.Item(i)
        Range("A1:B" & LR + 1).AutoFilter Field:=1, Criteria1:=CStr(ValUnique.Item(i))
        Range("B2:B" & LR + 1).SpecialCells(xlCellTypeVisible).Copy Cells(2, i + 3)
    Next i
    Range("A1:B1").Delete Shift:=xlUp
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1