Cookies help us deliver our services. By using our services, you agree to our use of cookies.
Tip: Other languages are Google-Translated. You can visit the English version of this link.
or

Register

or

## 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

###### Amazing! Using Efficient Tabs in Excel Like Chrome, Firefox and Safari!

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 20151207
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
xCol.Add xRg.Cells(i, 1).Value, xRg.Cells(i, 1).Value
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 60 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 > Content > 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 > Text > 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 200 handy Excel add-ins, free to try with no limitation in 60 days.

### Recommended Productivity Tools

#### Office Tab

Bring handy tabs to Excel and other Office software, just like Chrome, Firefox and new Internet Explorer.

#### Kutools for Excel

Amazing! Increase your productivity in 5 minutes. Don't need any special skills, save two hours every day!

300 New Features for Excel, Make Excel Much Easy and Powerful:

• Merge Cell/Rows/Columns without Losing Data.
• Combine and Consolidate Multiple Sheets and Workbooks.
• Compare Ranges, Copy Multiple Ranges, Convert Text to Date, Unit and Currency Conversion.
• Count by Colors, Paging Subtotals, Advanced Sort and Super Filter,
• More Select/Insert/Delete/Text/Format/Link/Comment/Workbooks/Worksheets Tools...

Say something here...
symbols left.
###### 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.
· 5 days ago
Hi can we add each row and give the output in one column, with the above functionality.
• To post as a guest, your comment is unpublished.
· 9 days ago
Need to get the same out put but for predefined columns to be selected would be (\$A,\$B) and need the output column Position on \$D\$1.
If any one have idea's that would be a great help!!!!
• To post as a guest, your comment is unpublished.
· 3 months ago
=INDEX(\$A\$2:\$A\$16, MATCH(0, COUNTIF(\$D\$1:\$D1, \$A\$2:\$A\$16), 0)) worked for me to transpose the unique values of a column into a new column BUT...is there a way to ad in a sort function so that the new column created is transposed in ascending order?

Thanks!
• To post as a guest, your comment is unpublished.
· 5 months ago
Same as Dave, I need to do the exactly opposite of this. Table 2 to transpose to Table 1. Input Table 2, Output Table 1.
• To post as a guest, your comment is unpublished.
· 6 months ago
I need to do exactly the opposite of this. I have many many columns associated with a row id and I want to paste them into two columns
for example I have
rowid, value, value1, value2, value3, value4, value..225
100, Dolphin, 255, 9--, sarah, jameson, ....
179, Router, flood, jason, 89, nose

I want it to look like this
100, Dolphin
100, 255
100, 9--
100, sarah
100, jaemeson
179, Router
179, flood
179, jason
179, 89
179, nose
• To post as a guest, your comment is unpublished.
· 5 months ago
Hello, Dave,
To solve your problem, please use the below VBA code: (Note: When you select the data range that you want to transpose, please exclude the header row.)

Sub TransposeUnique_2()
Dim xLRow, xLCount As Long
Dim xRg As Range
Dim xOutRg As Range
Dim xObjRRg 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:", "Kutools for Excel", xTxt, , , , , 8)
Set xRg = Application.Intersect(xRg, xRg.Worksheet.UsedRange)
If xRg Is Nothing Then Exit Sub
If (xRg.Rows.count < 2) Or _
(xRg.Areas.count > 1) Then
MsgBox "Invalid selection", , "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
Application.ScreenUpdating = False
xLCount = xRg.Columns.count
For xLRow = 1 To xRg.Rows.count
Set xObjRRg = Range(xRg.Cells(xLRow, 2), xRg.Cells(xLRow, xLCount)).SpecialCells(xlCellTypeConstants)
xObjRRg.Copy
xOutRg.Offset(, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Application.CutCopyMode = False
Range(Cells(xOutRg.Row, xOutRg.Column), Cells(xOutRg.Row + xObjRRg.count - 1, xOutRg.Column)).Value = xRg.Cells(xLRow, 1).Value
Set xOutRg = xOutRg.Offset(RowOffset:=xObjRRg.count)
Next
Application.ScreenUpdating = True
End Sub
• To post as a guest, your comment is unpublished.
· 3 months ago
Thank you, it works perfectly, you saved me 2 days! :)