vba - remove duplicates in a text string

monsierexcel

New Member
Joined
Nov 19, 2018
Messages
29
i have a string that has produced duplicate words

AC2 >"test grey grey white black white" what i want to produce> "test grey white black"
AC3> "blue green green black white black" what i want to produce> "blue green black white"

is there a way in VBA to remove duplicate words out of a string and present in the info as a formula for each row?
i have seen a function available to handle this but not sure how i present these functions for each row?

thank you guys
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I'm sure there's a more efficient method, but this should work...

Code:
Function Remove_Duplicate(cell As Range)

Dim str
Dim i As Long
str = Split(cell, " ")
For i = 0 To UBound(str)
    If InStr(1, Remove_Duplicate, str(i), 1) = 0 Then Remove_Duplicate = Remove_Duplicate & " " & str(i)
Next i
Remove_Duplicate = Trim(Remove_Duplicate)
End Function
 
Upvote 0
I'm sure there's a more efficient method, but this should work...

Code:
Function Remove_Duplicate(cell As Range)

Dim str
Dim i As Long
str = Split(cell, " ")
For i = 0 To UBound(str)
    If InStr(1, Remove_Duplicate, str(i), 1) = 0 Then Remove_Duplicate = Remove_Duplicate & " " & str(i)
Next i
Remove_Duplicate = Trim(Remove_Duplicate)
End Function
The following code may be more efficient (I think dictionaries are more efficient than repeated concatenations), however I do not think any speed difference that may exist will be noticed for what I assume are somewhat short text strings.
Code:
Function Uniques(Text As String) As String
  Dim X As Long, Data() As String
  Data = Split(Text)
  With CreateObject("Scripting.Dictionary")
    For X = 0 To UBound(Data)
      .Item(Data(X)) = 1
    Next
    Uniques = Join(.keys)
  End With
End Function
 
Upvote 0
@Rick, @njimack

The difference (I can see) between VBA codes is that njimack's code is not case sensitive, and Rick's, case sensitive.
 
Upvote 0
If the Op wants something that is case insensitive, this mod to Rick's code will do that.
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
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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