Avatar of finnstone
finnstone
 asked on

need to remove duplicate words in a cell

have a column of cells like this

ORIGINAL
world, industry, generation, industry
leader, leader, Covered, supplies
transformation, margin

NEED THIS TO BE
world, industry, generation
leader, Covered,
transformation, margin
VBA

Avatar of undefined
Last Comment
finnstone

8/22/2022 - Mon
Pawan Kumar

SELECT that column -> go to DATA tab -> CLICK remove duplicates--> You are done.
finnstone

ASKER
that is not correct
finnstone

ASKER
dupes only should be removed from within the same cell
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Pawan Kumar

Is it a one time activity ?
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Martin Liss

Ignore this if Bill's code works for you.

Otherwise add and run this macro. Note lines 8 and 9.

Sub RemoveCellDupes()
Dim lngLastRow As Long
Dim lngRow As Long
Dim colWords As Collection
Dim strWords() As String
Dim lngIndex As Long
Dim lngNextRow As Long
Const DATA_COL = "A" ' Change as needed
Const FIRST_ROW = 1 ' Change as needed

lngLastRow = Range(DATA_COL & "1048576").End(xlUp).Row
lngNextRow = FIRST_ROW

For lngRow = FIRST_ROW To lngLastRow
    Set colWords = New Collection
    strWords = Split(Cells(lngRow, DATA_COL), ",")
    For lngIndex = 0 To UBound(strWords) 
        On Error Resume Next
        colWords.Add Trim(strWords(lngIndex)), Trim(strWords(lngIndex))
    Next
    Cells(lngNextRow, DATA_COL).ClearContents
    For lngIndex = 1 To colWords.Count
        If lngIndex < colWords.Count Then
            Cells(lngNextRow, DATA_COL) = Cells(lngNextRow, DATA_COL) & colWords(lngIndex) & ", "
        Else
            Cells(lngNextRow, DATA_COL) = Cells(lngNextRow, DATA_COL) & colWords(lngIndex)
        End If
    Next
    lngNextRow = lngNextRow + 1
Next
End Sub

Open in new window

Subodh Tiwari (Neeraj)

Assuming each word in the cell is separated by a comma, you may give this a try...
Function getUniqueWords(ByVal vStr As String) As String
Dim str() As String
Dim dict
Dim i As Long

str = Split(Replace(Replace(vStr, Chr(10), ""), " ", ""), ",")
Set dict = CreateObject("Scripting.Dictionary")
For i = 0 To UBound(str)
    dict.Item(str(i)) = ""
Next i
If dict.Count > 0 Then getUniqueWords = Join(dict.keys, ", ")
End Function

Open in new window


Assuming your string is in A2 then use this user defined function on the sheet like below...
=getUniqueWords(A2)

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
finnstone

ASKER
awesome thanks Bill
finnstone

ASKER
Bill, got a new one. This time, I just need to remove dupes within the commas

https://www.experts-exchange.com/questions/29071630/remove-dupes-part-2.html
finnstone

ASKER
Your help has saved me hundreds of hours of internet surfing.
fblack61