Need a formula or Query to list out unique values

signup

New Member
Joined
Feb 15, 2018
Messages
42
Office Version
  1. 2019
Platform
  1. Windows
I have a sheet similar to the below data. Can we list out the unique names for each location? as shown in the below "desired output" section?


LocationNames
Location 1Name 1, Name 2, Name 4
Location 1Name 1, Name 4
Location 1Name 2, Name 3, Name 5
Location 1Name 1
Location 1Name 2
Location 1Name 6
Location 2Name 7, Name 1, Name 8
Location 2Name 2, Name 1
Location 2Name 2
Location 2Name 2, Name 9
Location 2Name 10
Location 3Name 11, Name 12, Name 1
Location 3Name 13
Location 3Name 1
Location 3Name 11
Location 3Name 1, Name 14

Desired Output:

LocationNames
Location 1Name 1, Name 2, Name 3, Name 4, Name 5, Name 6
Location 2Name 1, Name 2, Name 7, Name 8, Name 9, Name 10
Location 3Name 1, Name 11, Name 12, Name 13, Name 14

Thanks in advance!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Ok, how about
VBA Code:
Sub signup()
   Dim Ary As Variant, Sp As Variant
   Dim r As Long, i As Long
   
   With Sheets("Master")
      Ary = .Range("A1:B" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Ary(r, 2)
         Else
            Sp = Split(Ary(r, 2), ",")
            For i = 0 To UBound(Sp)
               If InStr(1, .Item(Ary(r, 1)), Trim(Sp(i)), 1) = 0 Then
                  .Item(Ary(r, 1)) = .Item(Ary(r, 1)) & ", " & Sp(i)
               End If
            Next i
         End If
      Next r
      Sheets("Sheet2").Range("A1").Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
   End With
End Sub
Change sheet names to suit.
 
Upvote 0
An alternative solution is to use Power Query to find the unique items. Mcode follows.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Column2", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Column2.1", "Column2.2", "Column2.3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column2.1", type text}, {"Column2.2", type text}, {"Column2.3", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Column1"}, "Attribute", "Value"),
    #"Trimmed Text" = Table.TransformColumns(#"Unpivoted Other Columns",{{"Value", Text.Trim, type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Trimmed Text",{"Attribute"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns")
in
    #"Removed Duplicates"

Then bring the query into power pivot and create a new measure


and then the final result

Book5
AB
1Column1New Value
2Location 1Name 1, Name 2, Name 4, Name 3, Name 5, Name 6
3Location 2Name 7, Name 1, Name 8, Name 2, Name 9, Name 10
4Location 3Name 11, Name 12, Name 1, Name 13, Name 14
Table1
 

Attachments

  • NewMeasure.JPG
    NewMeasure.JPG
    41.3 KB · Views: 9
Upvote 0
No VBA. Three steps, the best one was modified from stuff I found on stackexchange. textjoin based on location; the stakeexchange part turns that long string into a new array, then unique the array and join it back together.
:
MrExcelPlayground.xlsm
LMNOPQR
3LocationNames
4Location 1Name 1, Name 2, Name 4
5Location 1Name 1, Name 4
6Location 1Name 2, Name 3, Name 5
7Location 1Name 1
8Location 1Name 2LocationConcatenateduniquesAll in one formula
9Location 1Name 6Location 1Name 1, Name 2, Name 4, Name 1, Name 4, Name 2, Name 3, Name 5, Name 1, Name 2, Name 6Name 1, Name 2, Name 4, Name 3, Name 5, Name 6Name 1, Name 2, Name 4, Name 3, Name 5, Name 6
10Location 2Name 7, Name 1, Name 8Location 2Name 7, Name 1, Name 8, Name 2, Name 1, Name 2, Name 2, Name 9, Name 10Name 7, Name 1, Name 8, Name 2, Name 9, Name 10Name 7, Name 1, Name 8, Name 2, Name 9, Name 10
11Location 2Name 2, Name 1Location 3Name 11, Name 12, Name 1, Name 13, Name 1, Name 11, Name 1, Name 14Name 11, Name 12, Name 1, Name 13, Name 14Name 11, Name 12, Name 1, Name 13, Name 14
12Location 2Name 2
13Location 2Name 2, Name 9
14Location 2Name 10
15Location 3Name 11, Name 12, Name 1
16Location 3Name 13
17Location 3Name 1
18Location 3Name 11
19Location 3Name 1, Name 14
Sheet22
Cell Formulas
RangeFormula
P9:P11P9=TEXTJOIN(", ",TRUE,IF($L$4:$L$19=O9,$M$4:$M$19,""))
Q9:Q11Q9=TEXTJOIN(", ",TRUE,UNIQUE(TRIM(MID(SUBSTITUTE(P9,", ",REPT(" ",99)),IF(SEQUENCE(LEN(P9)-LEN(SUBSTITUTE(P9,", ",""))+1)=1,1,(SEQUENCE(LEN(P9)-LEN(SUBSTITUTE(P9,", ",""))+1)-1)*99),99))))
R9:R11R9=TEXTJOIN(", ",TRUE,UNIQUE(TRIM(MID(SUBSTITUTE(TEXTJOIN(", ",TRUE,IF($L$4:$L$19=O9,$M$4:$M$19,"")),", ",REPT(" ",99)),IF(SEQUENCE(LEN(TEXTJOIN(", ",TRUE,IF($L$4:$L$19=O9,$M$4:$M$19,"")))-LEN(SUBSTITUTE(TEXTJOIN(", ",TRUE,IF($L$4:$L$19=O9,$M$4:$M$19,"")),", ",""))+1)=1,1,(SEQUENCE(LEN(TEXTJOIN(", ",TRUE,IF($L$4:$L$19=O9,$M$4:$M$19,"")))-LEN(SUBSTITUTE(TEXTJOIN(", ",TRUE,IF($L$4:$L$19=O9,$M$4:$M$19,"")),", ",""))+1)-1)*99),99))))


The "R" column has it all in one nasty formula.
 
Upvote 0
Ok, how about
VBA Code:
Sub signup()
   Dim Ary As Variant, Sp As Variant
   Dim r As Long, i As Long
 
   With Sheets("Master")
      Ary = .Range("A1:B" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Ary(r, 2)
         Else
            Sp = Split(Ary(r, 2), ",")
            For i = 0 To UBound(Sp)
               If InStr(1, .Item(Ary(r, 1)), Trim(Sp(i)), 1) = 0 Then
                  .Item(Ary(r, 1)) = .Item(Ary(r, 1)) & ", " & Sp(i)
               End If
            Next i
         End If
      Next r
      Sheets("Sheet2").Range("A1").Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
   End With
End Sub
Change sheet names to suit.
Thank you for taking the time and sharing this code. This seems working but the original values are again repeating after the output.
I'm getting the output like below :

LocationNames
Location 1Name 1, Name 2, Name 3, Name 4, Name 5, Name 6
Location 2Name 1, Name 2, Name 7, Name 8, Name 9, Name 10
Location 3Name 1, Name 11, Name 12, Name 13, Name 14
Location 1Name 1, Name 2, Name 4
Location 1Name 1, Name 4
Location 1Name 2, Name 3, Name 5
Location 1Name 1
Location 1Name 2
Location 1Name 6
Location 2Name 7, Name 1, Name 8
Location 2Name 2, Name 1
Location 2Name 2
Location 2Name 2, Name 9
Location 2Name 10
Location 3Name 11, Name 12, Name 1
Location 3Name 13
Location 3Name 1
Location 3Name 11
Location 3Name 1, Name 14

Can we get the output in the new sheet? so that it would be clear enough to check as I have 400+ rows.
 
Upvote 0
@JamesCanale
The OP is using 2019 & therefore doesn't have the dynamic array functions you've used.
 
Upvote 0
Are you outputting the results to a new sheet as I did, or are you putting them on the same sheet as the data?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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