## How to transpose cells in one column based on unique values in another column?

Supposing, you have a range of data which contains two columns, now, you want to transpose cells in one column to horizontal rows based on unique values in another column to get the following result. Do you have any good ideas to solve this problem in Excel?

Transpose cells in one column based on unique values with formulas

Transpose cells in one column based on unique values with VBA code

With the following array formulas, you can extract the unique values and transpose their corresponding data into horizontal rows, please do as follows:

1. Enter this array formula: =INDEX(\$A\$2:\$A\$16, MATCH(0, COUNTIF(\$D\$1:\$D1, \$A\$2:\$A\$16), 0)) into a blank cell, D2, for example, and press Shift + Ctrl + Enter keys together to get the correct result, see screenshot:

Note: In the above formula, A2:A16 is the column that you want to list the unique values from, and D1 is the cell above this formula cell.

2. Then drag the fill handle down to the cells to extract all the unique values, see screenshot:

3. And then go on entering this formula into cell E2: =IFERROR(INDEX(\$B\$2:\$B\$16, MATCH(0, COUNTIF(\$D2:D2,\$B\$2:\$B\$16)+IF(\$A\$2:\$A\$16<>\$D2, 1, 0), 0)), 0), and remember to press Shift + Ctrl + Enter keys to get the result, see screenshot:

Note: In above formula: B2:B16 is the column data that you want to transpose, A2:A16 is the column that you want to transpose the values based on, and D2 contains the unique value that you have extracted in Step 1.

4. Then drag the fill handle to right of the cells that you want to list the transposed data until displays 0, see screenshot:

5. And then continue dragging the fill handle down to the range of cells to get the transposed data as following screenshot shown:

May be the formulas are complex for you to understand, here, you can run the following VBA code to get the desired result you need.

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 cells in one column based on unique values in another column:

``````Sub transposeunique()
'updateby Extendoffice
Dim xLRow As Long
Dim i As Long
Dim xCrit As String
Dim xCol  As New Collection
Dim xRg As Range
Dim xOutRg As Range
Dim xTxt As String
Dim xCount As Long
Dim xVRg As Range
On Error Resume Next
Set xRg = Application.InputBox("please select data range(only two columns):", "Kutools for Excel", xTxt, , , , , 8)
Set xRg = Application.Intersect(xRg, xRg.Worksheet.UsedRange)
If xRg Is Nothing Then Exit Sub
If (xRg.Columns.Count <> 2) Or _
(xRg.Areas.Count > 1) Then
MsgBox "the used range is only one area with two columns ", , "Kutools for Excel"
Exit Sub
End If
Set xOutRg = Application.InputBox("please select output range(specify one cell):", "Kutools for Excel", xTxt, , , , , 8)
If xOutRg Is Nothing Then Exit Sub
Set xOutRg = xOutRg.Range(1)
xLRow = xRg.Rows.Count
For i = 2 To xLRow
Next
Application.ScreenUpdating = False
For i = 1 To xCol.Count
xCrit = xCol.Item(i)
xOutRg.Offset(i, 0) = xCrit
xRg.AutoFilter Field:=1, Criteria1:=xCrit
Set xVRg = xRg.Range("B2:B" & xLRow).SpecialCells(xlCellTypeVisible)
If xVRg.Count > xCount Then xCount = xVRg.Count
xRg.Range("B2:B" & xLRow).SpecialCells(xlCellTypeVisible).Copy
xOutRg.Offset(i, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Application.CutCopyMode = False
Next
xOutRg = xRg.Cells(1, 1)
xOutRg.Offset(0, 1).Resize(1, xCount) = xRg.Cells(1, 2)
xRg.Rows(1).Copy
xOutRg.Resize(1, xCount + 1).PasteSpecial Paste:=xlPasteFormats
xRg.AutoFilter
Application.ScreenUpdating = True
End Sub
``````

3. Then press F5 key to run this code, and a prompt box will pop out to remind you select the data range that you want to use, see screenshot:

4. And then click OK button, another prompt box will pop out to remind you to select a cell to put the result, see screenshot:

6. Click OK button, and the data in column B has been transposed based on unique values in column A, see screenshot:

If you have Kutools for Excel, combining the Advanced Combine Rows and Split Cells utilities, you can quickly finish this task without any formulas or code.

 : with more than 300 handy Excel add-ins, free to try with no limitation in 30 days.

After installing Kutools for Excel, please do as follows:

1. Select the data range that you want to use. (If you want to keep the original data, please copy and paste the data to another location firstly.)

2. Then click Kutools > Merge & Split > Advanced Combine Rows, see screenshot:

3. In the Combine Rows Based on Column dialog box, please do the following operations:

(1.) Click the column name that you want to transpose data based on, and select Primary Key;

(2.) Click another column that you want to transpose, and click Combine then choose one separator to separate the combined data, such as space, comma, semicolon.

4. Then click Ok button, the data in column B has been combined together in one cell based on the column A, see screenshot:

5. And then select the combined cells, and click Kutools > Merge & Split > Split Cells, see screenshot:

6. In the Split Cells dialog box, select Split to Columns under the Type option, and then choose the separator which separate your combined data, see screenshot:

7. Then click Ok button, and select a cell to put the split result in the popped out dialog box, see screenshot:

8. Click OK, and you will get the result as you need. See screenshot:

Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days.

### Best Office Productivity Tools

 ðŸ¤– Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functionsâ€¦ Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ... Super Lookup: Multiple Criteria VLookup  |   Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup .... Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List .... Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ... Featured Features: Grid Focus   |  Design View   |   Big Formula Bar   |  Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells   |  Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ... Top 15 Toolsets:  12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

#### Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

• Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
• Open and create multiple documents in new tabs of the same window, rather than in new windows.
• Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Like many people in the below my column "b" has duplicates I still want to appear in a column, I.e.Â
How to do the transpose if B column doesn't have unique values but still need those values
KTE 100
KTE 100

Can you shared a modified equation that works in that scenario? I appears lots of people have this question below without an answer.
Thank you,Â
This comment was minimized by the moderator on the site
have you solve the problem?
This comment was minimized by the moderator on the site
Hello,Â Alicia,If there are duplicate values in the second column, you should apply the below array formula:=IFERROR(INDEX(\$B\$2:\$B\$16,SMALL(IF(\$D2=\$A\$2:\$A\$16,ROW(\$A\$2:\$A\$16)-ROW(\$A\$2)+1),COLUMN(A1))),"")
After inserting the formula, please remember to pressÂ Shift + Ctrl + Enter keys.
This comment was minimized by the moderator on the site
how would you do the first order but with multiple columns of data for each product? Like if KTO and KTE had multiple pieces of data in columns C, D, E,...

This was the formula used:

=IFERROR(INDEX(\$B\$2:\$B\$16, MATCH(0, COUNTIF(\$D2:D2,\$B\$2:\$B\$16)+IF(\$A\$2:\$A\$16<>\$D2, 1, 0), 0)), 0)
This comment was minimized by the moderator on the site
thanks !! just what i was looking for !! works as intended !!
This comment was minimized by the moderator on the site
this was a very, very helpful post - thank you!
I found the VBA version did not yield the expected results at least when running in VBA 7.1 (Excel for Office 365 - 16.0.x - 64-bit). I tweaked it a bit to get the results I wanted:

Sub transposeunique()

'updateby Extendoffice

'updateby skipow June 2020

Dim xLRow As Long

Dim i As Long

Dim xCrit As String

Dim xCritLast As String

Dim xCol As New Collection

Dim xRg As Range

Dim xOutRg As Range

Dim xTxt As String

Dim xCount As Long

Dim xVRg As Range

On Error Resume Next

Set xRg = Application.InputBox("please select data range(only two columns):", "Kutools for Excel", xTxt, , , , , 8)

Set xRg = Application.Intersect(xRg, xRg.Worksheet.UsedRange)

If xRg Is Nothing Then Exit Sub

If (xRg.Columns.Count <> 2) Or _

(xRg.Areas.Count > 1) Then

MsgBox "the used range is only one area with two columns ", , "Kutools for Excel"

Exit Sub

End If

Set xOutRg = Application.InputBox("please select output range(specify one cell):", "Kutools for Excel", xTxt, , , , , 8)

If xOutRg Is Nothing Then Exit Sub

Set xOutRg = xOutRg.Range(1)

xLRow = xRg.Rows.Count

For i = 2 To xLRow

'the above line commented out - the Add function to the Collection (at least in VBA 7.1) doesn't accept this format

'you only need the first column put into the Collection

Next

Application.ScreenUpdating = False

For i = 1 To xCol.Count

xCrit = xCol.Item(i)

'if you don't keep track of the last entry and compare to the next entry you'll get duplicate lines

If xCrit = xCritLast Then

xRg.AutoFilter

Else

xOutRg.Offset(i, 0) = xCrit

xRg.AutoFilter Field:=1, Criteria1:=xCrit

Set xVRg = xRg.Range("B2:B" & xLRow).SpecialCells(xlCellTypeVisible)

If xVRg.Count > xCount Then xCount = xVRg.Count

xRg.Range("B2:B" & xLRow).SpecialCells(xlCellTypeVisible).Copy

xOutRg.Offset(i, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True

Application.CutCopyMode = False

'save the last entry and compare above to the next one to avoid duplicates

xCritLast = xCrit

End If

Next

xOutRg = xRg.Cells(1, 1)

xOutRg.Offset(0, 1).Resize(1, xCount) = xRg.Cells(1, 2)

xRg.Rows(1).Copy

xOutRg.Resize(1, xCount + 1).PasteSpecial Paste:=xlPasteFormats

xRg.AutoFilter

Application.ScreenUpdating = True

End Sub
Zoe Â  Â  Gregg
This comment was minimized by the moderator on the site
This works, but it gives me duplicates. Is there a way to make it not?
This comment was minimized by the moderator on the site
it worked for me, i had to sort the first column though
This comment was minimized by the moderator on the site
can you please share the code if there are 2 columns to be copied instead of 1. below is the example.
There are no comments posted here yet