Remove Duplicates when concatenating in Excel 2016

sean1541

New Member
Joined
Feb 14, 2023
Messages
31
Office Version
  1. 2016
Platform
  1. MacOS
Hi how do I remove duplicates when concatenating I am trying to group allergens for a dish Allergens are in B1:B4
I want to concatenate in B5 with no duplicates
 

Attachments

  • Screenshot 2023-12-30 at 19.16.31.png
    Screenshot 2023-12-30 at 19.16.31.png
    84.2 KB · Views: 15

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
If a solution with UDF code works for you, you can try this code.

how it is used :

open vba application window with press ALT+ F11

insert / Module

Past code and close window

=combineuni(B1:B4)


=combineuni(B1:B4)


VBA Code:
Function combineuni(rng As Range) As String 'GB
    Dim cel As Range
    Dim uniqueWords As Collection
    Set uniqueWords = New Collection
    
    For Each cel In rng
        Dim words() As String
        words = Split(cel.Value, ",")
        
        Dim word As Variant
        For Each word In words
            On Error Resume Next
            uniqueWords.Add Trim(word), CStr(Trim(word))
            On Error GoTo 0
        Next word
    Next cel
    
    combineuni = Join(CollectionToArray(uniqueWords), ",")
End Function

Function CollectionToArray(col As Collection) As Variant()
    Dim arr() As Variant
    ReDim arr(1 To col.Count)
    Dim i As Integer
    For i = 1 To col.Count
        arr(i) = col.Item(i)
    Next i
    CollectionToArray = arr
End Function
 
Upvote 0
Excel 2016 means you don't have some of the more recent functions that would make this easier so I think that you would need a vba solution. Also, with MacOS, you also don't have access to some vba capabilities that might normally be used for a task like this.

However, I think that you could employ a fairly simple user-defined function like this. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function NoDupes(r As Range) As String
  Dim itm As Variant
  Dim c As Range
  
  For Each c In r
    For Each itm In Split(c.Value, ",")
      If InStr(1, NoDupes & ",", "," & itm & ",") = 0 Then NoDupes = NoDupes & "," & itm
    Next itm
  Next c
  NoDupes = Mid(NoDupes, 2)
End Function

sean1541.xlsm
B
1G,E
2S,MK,SP
3CY,SP
4G,F
5G,E,S,MK,SP,CY,F
Sheet1
Cell Formulas
RangeFormula
B5B5=NoDupes(B1:B4)
 
Upvote 1
Excel 2016 means you don't have some of the more recent functions that would make this easier so I think that you would need a vba solution. Also, with MacOS, you also don't have access to some vba capabilities that might normally be used for a task like this.

However, I think that you could employ a fairly simple user-defined function like this. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function NoDupes(r As Range) As String
  Dim itm As Variant
  Dim c As Range
 
  For Each c In r
    For Each itm In Split(c.Value, ",")
      If InStr(1, NoDupes & ",", "," & itm & ",") = 0 Then NoDupes = NoDupes & "," & itm
    Next itm
  Next c
  NoDupes = Mid(NoDupes, 2)
End Function

sean1541.xlsm
B
1G,E
2S,MK,SP
3CY,SP
4G,F
5G,E,S,MK,SP,CY,F
Sheet1
Cell Formulas
RangeFormula
B5B5=NoDupes(B1:B4)
Works perfect thanks a million
 
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,279
Members
449,094
Latest member
GoToLeep

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