Identify series of duplicate numbers in a cell

arunsri07

New Member
Joined
Oct 2, 2017
Messages
4
I have a datasheet with duplicates values in single cell. I would like to delete them using Macro.


Example:
108,125,98,90,113,64,89,71;61,38,57,53,51,30,39;29,25,19,7,5,61,38,57,53,51,30,39,29,25,19,7,5,61,38,57,53,51,30,39,29,25,19,7,5;61,38,57,53,51,30,39;29,25,19,7,5

<tbody>
</tbody>
34;19,12,9,6,19,12,9,6,19,12,9,6;19,12,9,6

<tbody>
</tbody>

<tbody>
</tbody>


Currently I am using this code, but it only seems to highlight duplicates, also only words not numbers.
Code:
[COLOR=#333333]Sub findspaces()[/COLOR]    lastrow = Range("A65536").End(xlUp).Row
    
    For I = 1 To lastrow
        'x = 0
        ask = Split(Cells(I, 1), " ")
        For j = LBound(ask) To UBound(ask) - 1
            x = 0
            For k = j + 1 To UBound(ask)
                If ask(j) = ask(k) Then
                    x = x + 1
                End If
            Next k
            
            If x > 0 Then
                pos = 0
                For L = 1 To x + 1
                    pos = InStr(pos + 1, Cells(I, 1), ask(j))
                    Cells(I, 1).Select
                    With ActiveCell.Characters(Start:=pos, Length:=Len(ask(j))).Font
                        .Color = -16776961
                    End With
                Next L
            End If
        Next j
    Next I
       [COLOR=#333333]End Sub[/COLOR]
Any suggestions would be very much appreciated.

Thanks in advance
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this for Duplicate Data in "A1" replaced by Unique Data in "A1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG02Oct38
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant, nstr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
Sp = Split(Replace(Range("A1"), ";", ","), ",")
[COLOR="Navy"]For[/COLOR] n = 0 To UBound(Sp)
    [COLOR="Navy"]If[/COLOR] Not .Exists(Sp(n)) [COLOR="Navy"]Then[/COLOR]
        nstr = nstr & IIf(nstr = "", Sp(n), ", " & Sp(n))
        .Add Sp(n), Nothing
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
Range("A1") = nstr
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this for Duplicate Data in "A1" replaced by Unique Data in "A1".
Code:
[COLOR=Navy]Sub[/COLOR] MG02Oct38
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range, n [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] Sp [COLOR=Navy]As[/COLOR] Variant, nstr [COLOR=Navy]As[/COLOR] [COLOR=Navy]String,[/COLOR] c [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
Sp = Split(Replace(Range("A1"), ";", ","), ",")
[COLOR=Navy]For[/COLOR] n = 0 To UBound(Sp)
    [COLOR=Navy]If[/COLOR] Not .Exists(Sp(n)) [COLOR=Navy]Then[/COLOR]
        nstr = nstr & IIf(nstr = "", Sp(n), ", " & Sp(n))
        .Add Sp(n), Nothing
    [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]Next[/COLOR]
Range("A1") = nstr
[COLOR=Navy]End[/COLOR] [COLOR=Navy]With[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick


Hi Mick,


It doesn't seem to work unfortunately. But thanks for looking into it, might have any other suggestions?
 
Upvote 0
Welcome to the MrExcel board!

Is what you have shown in the box in post #1 all in 1 cell, 2 cells, 3 cells, 4 cells (including a blank cell), or something else?

What are the expected results for the sample data you gave?

Do you really have some of the numbers separated by commas and some by semicolons?

When a suggestion does not do what you want, just saying "It doesn't work" doesn't give the helper much to go on!
- Did it produce the wrong results (in what way?)?
- Did it produce the right results but in the wrong place?
- Did it do nothing?
- Did it give an error message (details)?
- etc
 
Upvote 0
Try this for data in column "A".
NB:- If you run this code from the Macro Dialog Box, make sure your data sheet is visible
Code:
[COLOR="Navy"]Sub[/COLOR] MG02Oct30
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant, nstr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
Sp = Split(Replace(Dn.Value, ";", ","), ",")
[COLOR="Navy"]For[/COLOR] n = 0 To UBound(Sp)
    [COLOR="Navy"]If[/COLOR] Not .Exists(Sp(n)) [COLOR="Navy"]Then[/COLOR]
        nstr = nstr & IIf(nstr = "", Sp(n), ", " & Sp(n))
        .Add Sp(n), Nothing
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
    Dn.Value = nstr
    nstr = ""
    .RemoveAll
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this for data in column "A".
NB:- If you run this code from the Macro Dialog Box, make sure your data sheet is visible
Code:
[COLOR=Navy]Sub[/COLOR] MG02Oct30
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range, n [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] Sp [COLOR=Navy]As[/COLOR] Variant, nstr [COLOR=Navy]As[/COLOR] [COLOR=Navy]String,[/COLOR] c [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR=Navy]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Rng
Sp = Split(Replace(Dn.Value, ";", ","), ",")
[COLOR=Navy]For[/COLOR] n = 0 To UBound(Sp)
    [COLOR=Navy]If[/COLOR] Not .Exists(Sp(n)) [COLOR=Navy]Then[/COLOR]
        nstr = nstr & IIf(nstr = "", Sp(n), ", " & Sp(n))
        .Add Sp(n), Nothing
    [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]Next[/COLOR]
    Dn.Value = nstr
    nstr = ""
    .RemoveAll
[COLOR=Navy]Next[/COLOR] Dn
[COLOR=Navy]End[/COLOR] [COLOR=Navy]With[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick


Hi Mick,


Thank you so much it works perfectly.

Thank you
Arun
 
Upvote 0

Forum statistics

Threads
1,215,541
Messages
6,125,413
Members
449,223
Latest member
Narrian

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top