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
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.
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
'Updateby Extendoffice
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:
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.
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
'Updateby Extendoffice
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:
4. Then copy down the formula to your needed cells, and all the duplicate words have been removed from every cells. See screenshot:
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.
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!