0 votes
in VBA by Beginner (2 points)

I'm trying to acheive removing duplicate words for over 5k cells. I did some research on creating a macro to do this but nothing specific to mac users.

Once I find a code that can achieve this, how do I run the macro?

Hope I can find some help here.


2 Answers

0 votes
by Expert (892 points)
edited by

Try this UDF

Function RemoveDupes(txt As String, Optional delim As String = " ") As String
    Dim x As Variant

    With CreateObject("Scripting.Dictionary")
        .CompareMode = vbTextCompare
        For Each x In Split(txt, delim)
            If Trim(x) <> "" And Not .Exists(Trim(x)) Then .Add Trim(x), Nothing
        Next x
        If .Count > 0 Then RemoveDupes = Join(.Keys, delim)
    End With
End Function


I think if you are using MAC the first solution will not work for you so you can use this version

Function RemoveDupes(txt As String, Optional delim As String = " ") As String
    Dim x, c As New Collection, s As String
    With c
        For Each x In Split(txt, delim)
            On Error Resume Next
                c.Add Key:=x, Item:=Empty
                If Err.Number = 0 Then s = s & IIf(s = "", "", delim) & x Else x = vbNullString
            On Error GoTo 0
        Next x
        If .Count > 0 Then RemoveDupes = s
    End With
End Function


by Beginner (2 points)
Thanks! I still can't seem to get the macro to work , I mst be missing something.
by Expert (892 points)
How did you use the UDF? Through the worksheet or the VBE?

Simply try =RemoveDupes(A1,", ")
0 votes
by Beginner (16 points)

How about this simple macro? 

Sub RemoveDupes()
    Dim myrange As Range: Set myrange = Sheet4.Range("c1:c6")
    myrange.RemoveDuplicates Columns:=1, Header:=xlYes
End Sub


Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.


For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.