Removing duplicates from a string accounting for commas

bioplz

New Member
Joined
Dec 1, 2021
Messages
15
Office Version
  1. 2019
Platform
  1. Windows
I am using a userform to that is populated from an excel sheet and modified from another userform. I used this code to remove duplicates, but it doesn't account for commas.

VBA Code:
Function Uniques(Text As String) As String
  Dim X As Long, Data() As String
  Data = Split(Text)
  With CreateObject("Scripting.Dictionary")
    [COLOR=#0000ff].CompareMode=1[/COLOR]
    For X = 0 To UBound(Data)
      .Item(Data(X)) = 1
    Next
    Uniques = Join(.keys)
  End With
End Function

Is there any easy way to account for commas?

For example: 10.1, 10.2, 10.3, 10.4, 10.1, 10.2, 10.3
output with this code is: 10.1, 10.2, 10.3, 10.4, 10.3 because there is no comma after the second 10.3 it does not register as a duplicate.

I want the output to remove the last number and the commas. So, essentially, I want the code to remove the number and any following commas.
 

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"

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
12,310
I assume the comma is the delimiter, and the spaces are not part of the values (or in another way, the delimiter is ", "). So try:

VBA Code:
Function Uniques(Text As String) As String
  Dim X As Long, Data() As String
  Data = Split(Text, ",")
  With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    For X = 0 To UBound(Data)
      .Item(Trim(Data(X))) = 1
    Next
    Uniques = Join(.keys, ", ")
  End With
End Function
 
Solution

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,430
Office Version
  1. 2013
Platform
  1. Windows
Hi
Ty
VBA Code:
Function Uniques(Text As String) As String
  Dim X As Long, Data() As String
  Data = Split(Text, ",")
  With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    For X = 0 To UBound(Data)
      .Item(Trim(Data(X))) = 1
    Next
    Uniques = Join(.keys, ",")
  End With
End Function
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,430
Office Version
  1. 2013
Platform
  1. Windows
What
Seems same some how
Sorry Eric
 

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
12,310

ADVERTISEMENT

No worries, mohadin! We both analyzed it and came up with the same solution. I'm sure you posted yours before you had seen that I had posted mine. At the very least, it's a validation of the basic concept.
 

bioplz

New Member
Joined
Dec 1, 2021
Messages
15
Office Version
  1. 2019
Platform
  1. Windows
I assume the comma is the delimiter, and the spaces are not part of the values (or in another way, the delimiter is ", "). So try:

VBA Code:
Function Uniques(Text As String) As String
  Dim X As Long, Data() As String
  Data = Split(Text, ",")
  With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    For X = 0 To UBound(Data)
      .Item(Trim(Data(X))) = 1
    Next
    Uniques = Join(.keys, ", ")
  End With
End Function
Beautiful. Thank you.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,514
Messages
5,854,165
Members
431,623
Latest member
ncorkren

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
Top