Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in


How to transpose duplicate rows to columns in Excel?

Supposing you have a range of data in Excel, now, you would like to transpose the duplicate rows to multiple columns as following screenshot shown, do you have any good ideas to solve this task?

Transpose duplicate rows to columns with VBA code


Transform one column or row to a range or vice versa:
With Kutools for Excel’s Transform Range feature, you can quickly convert a range of cells to a single row or column, you can also convert a single row or column to a range of cells. Read more about this feature...
doc transpose range

arrow blue right bubble Transpose duplicate rows to columns with VBA code

Unfortunately, there is no direct way for you to deal with it in Excel, but you can create a VBA code to solve it, please do as follows:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Transpose duplicate rows to multiple columns

Sub ConvertTable()
'Update 20150113
Dim xArr1 As Variant
Dim xArr2 As Variant
Dim InputRng As Range, OutRng As Range
Dim xRows As Long
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Set OutRng = OutRng.Range("A1")
xArr1 = InputRng.Value
t = UBound(xArr1, 2): xRows = 1
With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    For i = 2 To UBound(xArr1, 1)
        If Not .exists(xArr1(i, 1)) Then
            xRows = xRows + 1: .Item(xArr1(i, 1)) = VBA.Array(xRows, t)
            For ii = 1 To t
                xArr1(xRows, ii) = xArr1(i, ii)
            xArr2 = .Item(xArr1(i, 1))
            If UBound(xArr1, 2) < xArr2(1) + t - 1 Then
                ReDim Preserve xArr1(1 To UBound(xArr1, 1), 1 To xArr2(1) + t - 1)
                For ii = 2 To t
                    xArr1(1, xArr2(1) + ii - 1) = xArr1(1, ii)
            End If
            For ii = 2 To t
                xArr1(xArr2(0), xArr2(1) + ii - 1) = xArr1(i, ii)
            xArr2(1) = xArr2(1) + t - 1: .Item(xArr1(i, 1)) = xArr2
        End If
End With
OutRng.Resize(xRows, UBound(xArr1, 2)).Value = xArr1
End Sub

3. Then press F5 key to run this code, select the data range that you want to convert the duplicate rows to multiple columns in the popped out dialog, see screenshot:


4. Click OK, and select one cell where you want to put the result in the following dialog box, see screenshot:


5. And then click OK button, your selected data has been converted to follows:


Related articles:

How to transpose / convert columns and rows into single row?

How to transpose / convert columns and rows into single column?

How to transpose / convert a single column to multiple columns in Excel?

Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!


Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Joe · 8 months ago
    This works fine for me with different number of duplicates Brian. I only had a problem with the first duplicate showing twice on my output, but that was very minor. I only wish I knew how to make it copy the duplicate results into a comma delimited format instead of new columns for each one.
  • To post as a guest, your comment is unpublished.
    Brian · 9 months ago
    Only works if there's the same number of duplicates and call him a. It doesn't work at if have different numbers of duplicates.
  • To post as a guest, your comment is unpublished.
    Alex · 2 years ago
    Really great. Saved me a lot of time
  • To post as a guest, your comment is unpublished.
    Jon · 3 years ago
    I am so incredibly happy this worked. You are my hero!!!!
  • To post as a guest, your comment is unpublished.
    Santosh · 4 years ago
    Awesome Example - saved me a lot of time.
    Thank You so much !!!