Copy unique values from two column in single column

sooshil

Board Regular
Joined
Feb 21, 2013
Messages
104
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have a table: Table1
AnyColumn1 and AnyColumn2 are two columns in Table1 which always have text values.
Those two columns may contain same text values in different rows as shown in picture.
Test.xlsm
BC
1AnyColumn1AnyColumn2
2Value1Value150
3Value2Value2
4Value3Value150
5Value150Value7
Sheet1


I want to copy unique values from both the columns and paste it into a given cell/range (1 column width) in ascending order.
The result of the above example should come like this.
Test.xlsm
G
4Value1
5Value150
6Value2
7Value3
8Value7
Sheet1


Thank you.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I want to copy unique values from both the columns

Here is a macro for you to consider,

VBA Code:
Sub unique_values()
  Dim coll As Object, c As Range, dict As Object, ky As Variant
  Set coll = CreateObject("System.Collections.ArrayList")
  Set dict = CreateObject("Scripting.Dictionary")
  For Each c In ActiveSheet.ListObjects("Table1").DataBodyRange
    dict(c.Value) = Empty
  Next
  For Each ky In dict.keys
    coll.Add ky
  Next
  coll.Sort
  Range("G4").Resize(dict.Count).Value = Application.Transpose(coll.toArray)
End Sub
 
Upvote 0
Here is a macro for you to consider,

VBA Code:
Sub unique_values()
  Dim coll As Object, c As Range, dict As Object, ky As Variant
  Set coll = CreateObject("System.Collections.ArrayList")
  Set dict = CreateObject("Scripting.Dictionary")
  For Each c In ActiveSheet.ListObjects("Table1").DataBodyRange
    dict(c.Value) = Empty
  Next
  For Each ky In dict.keys
    coll.Add ky
  Next
  coll.Sort
  Range("G4").Resize(dict.Count).Value = Application.Transpose(coll.toArray)
End Sub

Thank you so much DanteAmor
It worked.
 
Upvote 0
As another approach, if you were interested in a more direct, non-looping, method (more along the lines of HappierThan's suggestion) then you could try ..

VBA Code:
Sub UniqueAndsort()
  Dim DBR As Range
  
  Set DBR = ActiveSheet.ListObjects("Table1").DataBodyRange
  With Range("G4").Resize(DBR.Count)
    .Value = Application.Transpose(Split(Join(Application.Transpose(Evaluate(DBR.Columns(1).Address & "&""|""&" & DBR.Columns(2).Address)), "|"), "|"))
    .Sort Key1:=.Cells(1), Order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom
    .RemoveDuplicates Columns:=1, Header:=xlNo
  End With
End Sub
 
Upvote 0
another approach with Power Query
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    C2T = Table.FromList(Table.ToColumns(Source), Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Expand = Table.ExpandListColumn(C2T, "Column1"),
    Sort = Table.Sort(Table.Distinct(Expand),{{"Column1", Order.Ascending}})
in
    Sort
 
Upvote 0
another approach ...
Hmm, not sure if the OP will be interested in more approaches after this time, but since the thread has been resurrected, a formula approach is also possible:

sooshil 2020-06-30 1.xlsm
ABCD
1AnyColumn1AnyColumn2Unique List
2Value1Value150Value1
3Value2Value2Value150
4Value3Value150Value2
5Value150Value7Value3
6Value7
7 
8 
Sheet2
Cell Formulas
RangeFormula
D2:D8D2=IFERROR(INDIRECT(TEXT(AGGREGATE(15,6,(ROW(Table1)*10^6+COLUMN(Table1))/((Table1<>"")*(ISNA(MATCH(Table1,D$1:D1,0)))),1),"R000000C000000"),0),"")
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,810
Members
449,339
Latest member
Cap N

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