Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

How to Pivot Rows with Duplicate Values

Latest post Thu, Jun 17 2010 9:56 AM by denni50. 2 replies.
  • Wed, Jun 16 2010 3:10 PM

    • denni50
    • Top 500 Contributor
    • Joined on Wed, Jun 16 2010
    • Posts 4
    • Points 52

    How to Pivot Rows with Duplicate Values

    I know this will probably seem elementary to the more advanced crowd here, I'v written my fair share of VBA code and macros

    but I can't seem to get this to work.

    I need this:

             A                 B

    18928 438465
    19487 521378
    19487 617652
    44190 680961
    44190 701524
    44190 761229
    76456 693652
    76456 693685
    6159131 5484352
    6160141 7521815
    6160153 7623646
    6160489 7720745

    to look like this:

               A

    18928 438465
    19487 521378 617652
    44190 680961 701524 761229
    76456 693652 693685
    6159131 5484352
    6160141 7521815
    6160153 7623646
    6160489 7720745

     

     

     

    • Post Points: 21
  • Wed, Jun 16 2010 8:52 PM In reply to

    • Rick Williams
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Melbourne, Australia
    • Posts 76
    • Points 1,189

    Re: How to Pivot Rows with Duplicate Values

    I would suggest that the answer is in your question's title - use a pivot table.

     

    The first step would be to create a new column (column C) which provides a counter for the duplicate values in column A.

    Then generate a pivot table off columns A:C, with A as row headings, C as column headings and B as data.  Probably hide the 'grand totals' too - right click->table options-> uncheck "Grand totals for rows"/"columns"

     

    The formula you use in C could take one of two forms, if the duplicate values in column A are always sequential as in your example, you could use:

    C2:     =if($A2=$A1,C1+1,1)

    which simply increments on the value in the above cell if A has the same value.

     

    But if the duplicate values in Column A may be distributed amongst the whole list (i.e. unsorted) then a more complex (and computationally costly) expression like this can be used:

    C2:     =countif($A$2:$A2,$A2)

    which when filled down the list might make more sense.  It counts all occurences of value in column A on the current row above (and including) the current row. e.g. filled down to C10:

     

    C10:     =countif($A$2:$A10,$A10)

    i.e. "count the occurences of the value in A10 in range A2:A10"
    This can get a bit slow for very long lists (i.e. thousands or more) so if sorting is an option, go with the simpler formula.
    Cheers!
    Rick

     

    Rick Idea
    Melbourne, Australia

    Filed under: ,
    • Post Points: 21
  • Thu, Jun 17 2010 9:56 AM In reply to

    • denni50
    • Top 500 Contributor
    • Joined on Wed, Jun 16 2010
    • Posts 4
    • Points 52

    Re: How to Pivot Rows with Duplicate Values

    Hi Rick

    thanks a heap, I applied your recommendation and it worked.

    I initially tried using the Pivot Table feature in Excel but couldn't get the rows/data to pivot correctly so I attempted to write my own code and that became even more daunting, keep it simple is always the best solution.

    thanks again.

    Sum of DupID Column Labels    
    Row Labels 1 2 3
    18928 438465    
    19487 521378 617652  
    44190 680961 701524 761229
    76456 693652 693685  
    6159131 5484352  
    6160141 7521815  
    6160153 7623646  
    6160489 7720745    

     

     

    • Post Points: 5
Page 1 of 1 (3 items) | RSS
Copyright Excel User Group and the relevant contributors, 2010. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.