Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to remove duplicate characters or words in string of a cell?

Supposing there are duplicate characters or words in each cell, and you want to remove the duplicates and leave only unique characters or words. How could you solve this problem in Excel?

Remove duplicate characters of text string with User Defined Function

Remove duplicate words separated by punctuation with User Defined Function


Select duplicate values in a column and then remove them at once:

With Kutools for Excel’s Select Duplicate & Unique Cells feature, you can quickly select and highlight the duplicate or unique values from a range, and then you can remove or format them as you need.

doc remove both duplicates 15 15


arrow blue right bubble Remove duplicate characters of text string with User Defined Function


If you have a list of text string, now, you need to remove the duplicate characters as following screenshot shown. Here, I can talk about a VBA code to deal with it.

doc-remove-duplicate-characters-1

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Remove duplicate characters of text string in a cell

Function RemoveDupes1(pWorkRng As Range) As String
'Updateby20140924
Dim xValue As String
Dim xChar As String
Dim xOutValue As String
Set xDic = CreateObject("Scripting.Dictionary")
xValue = pWorkRng.Value
For i = 1 To VBA.Len(xValue)
    xChar = VBA.Mid(xValue, i, 1)
    If xDic.Exists(xChar) Then
    Else
        xDic(xChar) = ""
        xOutValue = xOutValue & xChar
    End If
Next
RemoveDupes1 = xOutValue
End Function

3. Then save and close this code, go back to the worksheet, and enter this formula =removedupes1(A2) (A2 is the data cell you want to remove duplicates from) into a blank cell besides your data, see screenshot:

doc-remove-duplicate-characters-1

4. And then drag the fill handle over to the range cells that you want to apply this formula, all the duplicate characters have been removed from each cell immediately.

doc-remove-duplicate-characters-1


arrow blue right bubble Remove duplicate words separated by punctuation with User Defined Function

If there are some words which are separated by certain punctuation marks in a cell, you can also remove the duplicate words as you like. Please do with the following steps:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Remove duplicate words separated by punctuation of a cell

Function RemoveDupes2(txt As String, Optional delim As String = " ") As String
    Dim x
    'Updateby20140924
    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
        If .Count > 0 Then RemoveDupes2 = Join(.keys, delim)
    End With
End Function

3. Then save and close this code, return to the worksheet, and enter this formula =RemoveDupes2(A2,",") into a blank cell next to your data, see screenshot:

doc-remove-duplicate-characters-1

4. Then copy down the formula to your needed cells, and all the duplicate words have been removed from every cells. See screenshot:

doc-remove-duplicate-characters-1

Note: In the above formula, A2 indicates the cell that you want to use, and the comma (,) stands for the punctuation mark which separates the words in a cell, you can change it to any other punctuations to your need.


Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Rocio · 4 months ago
    Hi, what should I do if I want to delete duplicated words within on cell? For example, Ana Garcia Lopes Garcia -> Ana Garcia Lopes
    • To post as a guest, your comment is unpublished.
      skyyang · 4 months ago
      Hello, Rocio,
      To remove the duplicate words within a cell, you should apply the User Defined Function of the second method in this article,
      Remove duplicate words separated by punctuation with User Defined Function

      and then apply this formula:=RemoveDupes2(A2," ").
      Please try it, hope it can help you!
      • To post as a guest, your comment is unpublished.
        Rocio · 4 months ago
        Thanks! but what will happen if i do not have punctuation within the cell? for example: Apple Book Pencil Book and I want to remove the duplicated word in this case Book and just obtain Apple Book Pencil
        • To post as a guest, your comment is unpublished.
          skyyang · 4 months ago
          Hi,
          If your cell value is separated by space, after pasting the above code, you just need to replace the comma with space as follows:
          Replace this formula:=RemoveDupes2(A2,",") with =RemoveDupes2(A2," ")
  • To post as a guest, your comment is unpublished.
    Bill Davis · 4 months ago
    I have been using the VBA code: Remove duplicate words separated by punctuation of a cell RemoveDupes2 logic for some time now and I recently had my machine upgraded to O365 and when running this same process, I get an error on my cell. Instead of returning the values I expect, I am getting the #Value! error in the cell.

    I'm not too savy on writing formulas or using the VB logic, so not sure where to start, any guidance would be helpful.
  • To post as a guest, your comment is unpublished.
    NeedExcelHelp · 8 months ago
    What about removing duplicate addresses. If I have a cell that says "123 Address St. 123 Address St." How can I select only the first address when I have many cells with different addresses in each of them?
  • To post as a guest, your comment is unpublished.
    Suraj Singh · 8 months ago
    Can we removed the duplicate from in single cell tax . like as per below tax the highlighted tax is duplicate.

    C2/1104, SUMMER PALM SECTOR 86 C2/1104, SUMMER PALM FARIDABAD HARYANA India
  • To post as a guest, your comment is unpublished.
    Angshuman Pradhan · 9 months ago
    how to save =RemoveDupes2 this formula in excel 2007
    • To post as a guest, your comment is unpublished.
      skyyang · 9 months ago
      Hello, Pradhan,
      To save this formula for using next time, you should save the workbook as Excel Macro-Enabled Workbook file format.
      Please try it, hope it can help you!