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 (916 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 (916 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
Ask any questions you have about VBA and Python and our community will help answer them. wellsr Q&A is the standalone question and answer platform for wellsr.com. If you have a question about one of our specific tutorials, please include a link back to the tutorial.

Getting Started
VBA Cheat Sheets (On Sale Now)

Looking for something else? Hire our team directly through ourVBA Help page, instead.

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