Unique in Range

How_Do_I

Well-known Member
Joined
Oct 23, 2009
Messages
1,750
Hello, how would I count the unique in a range like this and extract them in to a vertical list please...?

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
Excel Workbook
BCDE
2Plum
3PlumWatermelonApplePeach
4MelonKiwifruitBlackberry
5RaspberryBlueberryCranberryBanana
Sheet1
[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif][/FONT]
 

How_Do_I

Well-known Member
Joined
Oct 23, 2009
Messages
1,750
Found out how to count the unique...

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]G2=SUMPRODUCT((B2:E5<>"")/COUNTIF(B2:E5,B2:E5&""))

Just need how to extract the unique now...

[/FONT]
 

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,588
Office Version
365, 2019, 2016
Platform
Windows
VBA or Power Query both give the following results.

<b></b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)"><colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /><col /></colgroup><thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>F</th><th>G</th></tr></thead><tbody><tr ><td style="color: rgb(22,17,32);text-align: center;">1</td><td style=";">Fruit</td><td style=";">Count</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">2</td><td style=";">Plum</td><td style="text-align: right;;">2</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">3</td><td style=";">Watermelon</td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">4</td><td style=";">Apple</td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">5</td><td style=";">Peach</td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">6</td><td style=";">Melon</td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">7</td><td style=";">Kiwifruit</td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">8</td><td style=";">Blackberry</td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">9</td><td style=";">Raspberry</td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">10</td><td style=";">Blueberry</td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">11</td><td style=";">Cranberry</td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">12</td><td style=";">Banana</td><td style="text-align: right;;">1</td></tr></tbody></table><p style="width:4.8em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(187,187,187);border-top:none;text-align: center;background-color: rgb(218,231,245);color: rgb(22,17,32)">Sheet2</p><br /><br />

VBA:
Code:
Sub UNIQUECOUNTS()
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim AR() As Variant:    AR = Range("B2:E" & Range("B" & Rows.Count).End(xlUp).Row).Value
Dim R As Range:         Set R = Range("G2")


For i = 1 To UBound(AR)
    For j = 1 To UBound(AR, 2)
        If AR(i, j) <> vbNullString Then
            If Not SD.exists(AR(i, j)) Then
                SD.Add AR(i, j), 1
            Else
                SD(AR(i, j)) = SD(AR(i, j)) + 1
            End If
        End If
    Next j
Next i


R.Resize(SD.Count, 1) = Application.Transpose(SD.keys)
R.Resize(SD.Count, 1).Offset(, 1) = Application.Transpose(SD.items)
End Sub
Power Query:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    List = Table.AddColumn(Source, "Custom", each Record.FieldValues(_)),
    Remove = Table.RemoveColumns(List,{"Column1", "Column2", "Column3", "Column4"}),
    Expanded = Table.ExpandListColumn(Remove, "Custom"),
    NoNull = Table.SelectRows(Expanded, each [Custom] <> null and [Custom] <> ""),
    Group = Table.Group(NoNull, {"Custom"}, {{"Count", each Table.RowCount(_), type number}}),
    Rename = Table.RenameColumns(Group,{{"Custom", "Fruit"}})
in
    Rename
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
42,205
Office Version
365
Platform
Windows
Just need how to extract the unique now...
For a more concise formula approach than the link in post 2, this formula copied down as far as you might ever need.
It does use the volatile function INDIRECT but unless your list could be at least tens of thousands of unique items, that shouldn't cause a performance problem.

This also then gives a nice easy formula for the unique count.

<b>Sheet1</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Arial,Arial; font-size:8pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:78px;" /><col style="width:89px;" /><col style="width:75px;" /><col style="width:81px;" /><col style="width:18px;" /><col style="width:18px;" /><col style="width:104px;" /><col style="width:104px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td><td >H</td><td >I</td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Unique List</td><td style="font-size:10pt; text-align:center; ">Count Unique</td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="font-size:10pt; ">Plum</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Plum</td><td style="font-size:10pt; text-align:center; ">11</td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="font-size:10pt; ">Plum</td><td style="font-size:10pt; ">Watermelon</td><td style="font-size:10pt; ">Apple</td><td style="font-size:10pt; ">Peach</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Watermelon</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="font-size:10pt; ">Melon</td><td style="font-size:10pt; ">Kiwifruit</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Blackberry</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Apple</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="font-size:10pt; ">Raspberry</td><td style="font-size:10pt; ">Blueberry</td><td style="font-size:10pt; ">Cranberry</td><td style="font-size:10pt; ">Banana</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Peach</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Melon</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Kiwifruit</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Blackberry</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Raspberry</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Blueberry</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Cranberry</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; ">Banana</td><td style="font-size:10pt; "> </td></tr><tr style="height:21px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td><td style="font-size:10pt; "> </td></tr></table><br /><table style="font-family:Arial; font-size:10pt; border-style: groove ;border-color:#00ff00;background-color:#fffcf9; color:#000000; "><tr><td ><b>Spreadsheet Formulas</b></td></tr><tr><td ><table border = "1" cellspacing="0" cellpadding="2" style="font-family:Arial; font-size:9pt;"><tr style="background-color:#cacaca; font-size:10pt;"><td >Cell</td><td >Formula</td></tr><tr><td >H2</td><td >=IFERROR(INDIRECT<span style=' color:008000; '>(TEXT<span style=' color:#0000ff; '>(AGGREGATE<span style=' color:#ff0000; '>(15,6,<span style=' color:#804000; '>(ROW<span style=' color:#ff7837; '>(B$2:E$5)</span>*10^6+COLUMN<span style=' color:#ff7837; '>(B$2:E$5)</span>)</span>/<span style=' color:#804000; '>(<span style=' color:#ff7837; '>(B$2:E$5<>"")</span>*<span style=' color:#ff7837; '>(ISNA<span style=' color:#8000ff; '>(MATCH<span style=' color:#545fa5; '>(B$2:E$5,H$1:H1,0)</span>)</span>)</span>)</span>,1)</span>,"R000000C000000")</span>,0)</span>,"")</td></tr><tr><td >I2</td><td >=COUNTIF(H2:H20,"?*")</td></tr></table></td></tr></table>
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
7,149
Office Version
2019
Platform
Windows
Something that I just tried in another thread inspired this idea.

Using the same layout as Peter in the previous reply, try this array formula in H2, confirmed with Ctrl Shift Enter

=TRIM(LEFT(TEXTJOIN(REPT(" ",255),1,IF(ISERROR(MATCH($B$2:$E$5,H$1:H1,0)),$B$2:$E$5&"","")),255))
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
42,205
Office Version
365
Platform
Windows
Something that I just tried in another thread inspired this idea.

Using the same layout as Peter in the previous reply, try this array formula in H2, confirmed with Ctrl Shift Enter

=TRIM(LEFT(TEXTJOIN(REPT(" ",255),1,IF(ISERROR(MATCH($B$2:$E$5,H$1:H1,0)),$B$2:$E$5&"","")),255))
Some comments
1. If the data is like the sample then 255 is much bigger than needed for the number of spaces. Only really needs to be as big as the longest expected text in the list. This is relevant as TEXTJOIN has a character limit of 32,767.
With data like the sample, that limit could be exceeded with a range of, say, 4 columns x 35 rows. Reducing the 255 to, say, 50 would allow a considerably larger range before exceeding the character limit.

2. Probably unlikely, but if the target range (B2:E5 here) contained any error value like #VALUE ! or #DIV/0! then the suggested formula will not return any values. Swapping ISERROR for ISNA in the suggested formula would avoid that for all errors except #N/A! errors

3. If the OP's signature is up-to-date, a TEXTJOIN solution will not be viable. :)
 

How_Do_I

Well-known Member
Joined
Oct 23, 2009
Messages
1,750
Thanks everyone... yes Peter_SSs I'm using XL 2010 so TEXTJOIN isn't available...
 

Forum statistics

Threads
1,081,617
Messages
5,360,044
Members
400,565
Latest member
Tommy O

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top