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

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:

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

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
xTxt = ActiveWindow.RangeSelection.Address
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:

Transpose cells in one column based on unique values with Kutools for Excel

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.

Kutools for Excel: 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:

Demo: Transpose cells in one column based on unique values with Kutools for Excel

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

The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...

Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...

Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...

Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...

Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...

Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...

Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...

More than 300 powerful features. Supports Office/Excel
2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features
30-day free trial. 60-day money back guarantee.

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
xTxt = ActiveWindow.RangeSelection.Address 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 'the above line commented out - the Add function to the Collection (at least in VBA 7.1) doesn't accept this format
xCol.Add Item:=xRg.Cells(i, 1).Value '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

The problem i am facing is that for some data in column A i don't have values for each year only for some.(For example country 2 has missing values for Year 2)

Is there a way to work around this issue and resolve it?

I have a data set which has multiple IDs in column A, and has connected data in column B. I used the above formula and altered it a bit so that I am transposing the cells in the column B into a row based on the unique ID tied to it in column A. The formula used to identify the unique IDs is: =INDEX($A$2:$A$13409, MATCH(0, COUNTIF($D$1:$D1, $A$2:$A$13409), 0)). The formula used to do the transposing is: =IFERROR(INDEX($B$2:$B$13409, MATCH(0, IF($A$2:$A$13409<>$D2, 1, 0)+COUNTIF($D2:D2,$B$2:$B$13409), 0)), "N/A"). Both given in the article, only slightly altered.

The issue is my data set in column B has duplicates, sometimes appearing one after another, and I need all of the values in the column to be presented in the rows.

The image attached is what I would like the table to show (this is a small sample size, the true dataset has over 13,000 entries). What is happening now is when a repeat value is encountered, it will not count it.
i.e. Row 9 for ID 11980 now only shows 0 -31.79 -0.19 -0.74 N/A N/A .... when what I need it to show instead is 0 0 -31.79 -0.19 -0.74 0 0 N/A N/A ....

Is there a way to work around this issue and resolve it?

I have a data set in Columns A (Unique ID) - E. Each row has data based on the ID#, there are multiple rows for each ID# but I want one row per ID# with all of the other data in columns (it would be 5 columns long minimum and 25 maximum depending on how many each unique ID has). I found a code but it only works for two columns. I had to concatenate the four columns (not including ID) then delimit after running the macro (lot of work). For 15,000 rows of data this is extra time consuming. Is there an endless column macro that would work? Thanks in advance everyone for your help!
ID CODE ST CODE# DATE

=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 get the all the values in B column to be transposed as given below:

Product Order Date Product Order Order Order Order Order Order Order
KTE 100 3/3/2019 KTE 100 100 100 200 100 150 100
KTO 150 3/3/2019 KTO 150 100 200 100 150 200
KTE 100 3/4/2019 BOT 150 100 200 150 100 200
KTO 100 3/4/2019 COD 200 150 100 150
KTO 200 3/5/2019
KTE 100 3/5/2019
BOT 150 3/5/2019
BOT 100 3/6/2019
KTO 100 3/6/2019
KTE 200 3/6/2019
BOT 200 3/7/2019
COD 200 3/7/2019
KTE 100 3/7/2019
KTO 150 3/7/2019
BOT 150 3/8/2019
KTE 150 3/8/2019
COD 150 3/8/2019
BOT 100 3/9/2019
BOT 200 3/10/2019
COD 100 3/10/2019
KTO 200 3/10/2019
COD 150 3/11/2019
KTE 100 3/11/2019

So I am working for a company. We have columns for info such as Last name, first name, rank, section, phone number, address. Is there a way I can use a similar formula to transpose the entire row of info to a column by names?

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

=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?

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

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 xTxt = ActiveWindow.RangeSelection.Address
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

Hello, ygoyal,
To solve your problem, please apply the below code:
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
Dim xC, xI, xI1, xI2 As Integer
On Error Resume Next
xTxt = ActiveWindow.RangeSelection.Address
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)
On Error Resume Next
xC = (xObjRRg.Count Mod 2)
If xC <> 0 Then
xC = Int(xObjRRg.Count / 2) + 1
Else
xC = Int(xObjRRg.Count / 2)
End If
xI1 = 1
xI2 = 2
For xI = 1 To xC
Range(xObjRRg.Item(xI1), xObjRRg.Item(xI2)).Copy
xOutRg.Offset(, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
xOutRg.Value = xRg.Cells(xLRow, 1).Value
Set xOutRg = xOutRg.Offset(RowOffset:=1)
xI1 = xI1 + (2)
xI2 = xI2 + (2)
Next
Next
Application.ScreenUpdating = True
End Sub

Hi, ygoyal, Sorry for replying late, please apply the following code, please try it!

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
Dim xFRg, xSRg, xCRg As Range On Error Resume Next
xTxt = ActiveWindow.RangeSelection.Address Set xRg = Application.InputBox("please select data range(only 3 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 <> 3) 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 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
Set xSRg = xRg.Range("B2:B" & xLRow).SpecialCells(xlCellTypeVisible) Set xCRg = xOutRg.Offset(i, 1)
For Each xFRg In xSRg xFRg.Copy
xCRg.PasteSpecial xRg.Range("B1").Copy
xCRg.Offset(-(i), 0).PasteSpecial xFRg.Offset(0, 1).Copy
Set xCRg = xCRg.Offset(0, 1) xCRg.PasteSpecial
xRg.Range("c1").Copy xCRg.Offset(-(i), 0).PasteSpecial
Set xCRg = xCRg.Offset(0, 1) Next
Application.CutCopyMode = False Next
xRg.Item(1).Copy xOutRg.PasteSpecial
xRg.AutoFilter Application.ScreenUpdating = True
End Sub

Hey Bro I tried using this code but the excel goes hang when I run this code and could not see the output from the above code. please suggest what to do in this case.

How can I transpose the data using approximate matches? Say, I want to extract all the values from Column B that match the first 9 characters / digits from Column A? Column B has 11 characters while A only 9. thank you!

How to do the transpose if B column doesn't have unique values but still need those values
KTE 100
KTE 100
Assuming that they are two different transaction

Hello, guys,
To get the opposite result based on the example of this article, you can apply the following VBA code: (Note:When selecting 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 xTxt = ActiveWindow.RangeSelection.Address
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