Remove repeated values within single cell?

Is there a formula to remove duplicate values from single cell?

Current cell value:    "Line 7; 7; 7; 7; 8; 9; John Smith"
End result to be:       "Line 7; 8; 9; John Smith"

Number of duplicates is anywhere from 0-50, and always different number values (could be any number instead of "7" from this example)
Sasha2016Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
You can use this macro. It assumes the data is in column 'A'. If it isn't then change line 5 to suite.

Sub RemoveDupes()
Dim strParts() As String
Dim lngLastRow As Long
Dim lngRow As Long
Const DATA_COL = "A"
Dim colUnique As Collection
Dim lngEntry As Long


Application.ScreenUpdating = False

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

For lngRow = 1 To lngLastRow
    Set colUnique = New Collection
    strParts = Split(Cells(lngRow, DATA_COL), "; ")
    For lngEntry = 0 To UBound(strParts)
        On Error Resume Next
        colUnique.Add strParts(lngEntry), CStr(strParts(lngEntry))
        On Error GoTo 0
    Next

    Cells(lngRow, DATA_COL) = colUnique(1)
    For lngEntry = 3 To colUnique.Count
        Cells(lngRow, DATA_COL) = Cells(lngRow, DATA_COL) & "; " & colUnique(lngEntry)
    Next
Next

Application.ScreenUpdating = True

End Sub

Open in new window


Just in case you need them, here are instructions on how to install and use the macro.

In Excel, Press Alt+F11 to open the Visual Basic Editor (VBE)

Right-click on your workbook name in the "Project-VBAProject" pane. If you don’t see an existing module then select Insert -> Module from the menu bar. Otherwise just select the module.

Copy the macro (you can use the ‘Select All’ button if you like) and paste it into the right-hand pane of the VBA editor ("Module1" window)

Press Alt+F11 again to go back to Excel

Optionally, press Alt+F8 to open the "Macro" dialog window. Select the macro, click ‘Options…’,  hold down the Shift key and type the letter A (or any other letter) and click ‘OK’. Then anytime you want to run the macro press Ctrl+Shift+A.

When you close the workbook you will need to save it as an xlsm or xls file if it’s not already one of those.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sasha2016Author Commented:
Hi Martin,
thanks for looking into this!

I'm getting invalid call or procedure on this line:
 Cells(lngRow, DATA_COL) = colUnique(1)

All I changed was data column from "A" to "O"  (line 5 in your macro)
0
Sasha2016Author Commented:
Data I'm trying to modify is starting in O6 and number of row varies, but is never past O1500.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Not for points.
That's why you are supposed to provide all the required information in your original post.
Here is the tweaked code. See if that works for you.

Sub RemoveDupes()
Dim strParts() As String
Dim lngLastRow As Long
Dim lngRow As Long
Const DATA_COL = "O"
Dim colUnique As Collection
Dim lngEntry As Long


Application.ScreenUpdating = False

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

For lngRow = 6 To lngLastRow
    If Cells(lngRow, DATA_COL) <> "" Then
        Set colUnique = New Collection
        strParts = Split(Cells(lngRow, DATA_COL), "; ")
        For lngEntry = 0 To UBound(strParts)
            On Error Resume Next
            colUnique.Add strParts(lngEntry), CStr(strParts(lngEntry))
            On Error GoTo 0
        Next
    
        Cells(lngRow, DATA_COL) = colUnique(1)
        For lngEntry = 3 To colUnique.Count
            Cells(lngRow, DATA_COL) = Cells(lngRow, DATA_COL) & "; " & colUnique(lngEntry)
        Next
    End If
Next

Application.ScreenUpdating = True

End Sub

Open in new window

0
Sasha2016Author Commented:
Thanks!
This worked, with one minor tweak afterwards...if there are no duplicate numbers, it removes "John Smith" from the string.
i.e.  "Line 1; John Smith" is trimmed to "Line 1"....but any cells that had duplicates now look correct.
I'm just re-adding name if cell LEN <7.

(Sorry I didn't post sample spreadsheet - it started off as a question about possible formula for single cell)
0
Sasha2016Author Commented:
Thank you both for your help
0
Martin LissOlder than dirtCommented:
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
              Experts Exchange Top Expert VBA (current)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.