Transpose Unique Values in column to row

D3allamerican07

Board Regular
Joined
Jul 22, 2015
Messages
101
Column A on Sheet1 has many different remarks. I need to make a unique list on Sheet2 with values from Column A on Sheet1 but post it in one row. This is the idea:

Sheet1:
Column A
1
1
2
2
2
3
3
3

Sheet2:
Column A------------Column B------------Column C
1-------------------------2----------------------3

The code I have doesn't quite work:
Code:
Sheets("Sheet1").Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
    Selection.Copy
    Range("B1").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
 
Thanks DanteAmor.
But code is not getting executed properly. It is taking some values of column A value to column D also. and it is not considering all the data from A2:C.

You need to check your data, maybe you have strange characters between the cells, blank spaces after the words, combined cells. They are just assumptions, all macros are working with sample data, but all macros are failing with your data.

Another attempt, this one ensures reading only A:C data:

VBA Code:
Sub TransposeColumns()
  Dim a As Variant, b() As Variant
  Dim dic As Object, i As Long, lin As Long, col As Long, n As Long
  Dim lr As Long
  
  lr = ActiveSheet.Range("A:C").Find("*", , xlValues, , xlByRows, xlPrevious).Row
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2:C" & lr).Value2
  Range("D2", Cells(Rows.Count, Columns.Count)).ClearContents
  
  For i = 1 To UBound(a)
    dic(a(i, 1)) = dic(a(i, 1)) + 1
    If dic(a(i, 1)) > n Then n = dic(a(i, 1))
  Next
  n = (n * 2) + 1
  ReDim b(1 To dic.Count, 1 To n)
  dic.RemoveAll
  For i = 1 To UBound(a)
    If Not dic.exists(a(i, 1)) Then
      lin = lin + 1
      col = 1
      b(lin, col) = a(i, 1)
    Else
      lin = Split(dic(a(i, 1)), "|")(0)
      col = Split(dic(a(i, 1)), "|")(1) + 2
    End If
    dic(a(i, 1)) = lin & "|" & col
    b(lin, col + 1) = a(i, 2)
    b(lin, col + 2) = a(i, 3)
  Next
  Application.ScreenUpdating = False
  Range("E2").Resize(dic.Count, n).Value = b
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Thanks bro the code is working fine. But getting 1 duplicate in between the newly transposed data.
Thanks bro the code is working fine. But getting 1 duplicate in between the newly transposed data.
Hi Bro, The code is working fine but in case there is only 1 type of value in column B then it is showing error. Attaching same for your reference.
 

Attachments

  • 1590656328348.png
    1590656328348.png
    7.2 KB · Views: 27
  • 1590656353200.png
    1590656353200.png
    8.5 KB · Views: 28
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top