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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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
 
Upvote 0
Solution
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
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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