Duplicate values in a range, using VBA

timburbidge

New Member
Joined
May 16, 2005
Messages
23
Hi All

I'm looking for a formula/VBA code which will look through a range of values eg

T1Bc2
T1Ba1
T1Ba1
T1Ad1
T2Ea2
T2Ea2
T1Ab1
T1Ac1
T1Ba2
T1Ac3
T2Ea4
T2Ec4
T1Bb2
T1Ab2
T1Ab2
T1Ab2
T1Ab2
T1Ac1
T1Bc2

and return those values which appear more than once. So, if T1Ab2 appears 3 times, the result would be T1Ab2 = 2. Or, if none of the codes are duplicated, the result would be a nil return

Thanks very much

Tim
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
There is probably a shorter way of doing it via code, but this seems to work for me. This is assuming the list starts in A1 and you want to place the results in column B:

Code:
Sub asdf()
Dim i As Long, LastRow As Long
Dim temp As Variant, x As Long

LastRow = Columns(1).Find("*", searchdirection:=xlPrevious).Row

For i = 1 To LastRow
    x = WorksheetFunction.CountIf(Range("A1:A" & LastRow), Cells(i, 1))
    If x > 1 Then
        If temp = "" Then
            temp = Cells(i, 1) & "=" & x
        Else
            If InStr(1, temp, Cells(i, 1)) = 0 Then
                temp = temp & "," & Cells(i, 1) & "=" & x
            End If
        End If
    End If
Next i

If temp = "" Then
    [B1] = 0
Else
    temp = Split(temp, ",")
    For i = LBound(temp) To UBound(temp)
        Cells(i + 1, 2) = temp(i)
    Next i
End If

End Sub

Alternately, you could just use a simple formula in column B to "tag" whether or not a cell is a duplicate. This would go in B1 and then you can just drag it down the column:
=IF(COUNTIF($A$1:$A$19,A1)>1,"Duplicate","")
 
Upvote 0
thanks very much. i'll have a look through it and try to decipher. if you have time, some narrative in between the lines wouldn't hurt (cheeky!)
 
Upvote 0
Oh, no problem. I usually do that anyway; not sure why I didn't on this one.

At any rate, here's the exact same code with some comments to (hopefully) explain:
Code:
Sub asdf()
Dim i As Long, LastRow As Long
Dim temp As Variant, x As Long

'find last row with data in column A
'this is used as the stopping point of the list
LastRow = Columns(1).Find("*", searchdirection:=xlPrevious).Row

'assuming the data starts in row 1 and goes until the LastRow
'loop through the rows in the column
For i = 1 To LastRow
    'count the number of times the value of the cell appears in the range
    x = WorksheetFunction.CountIf(Range("A1:A" & LastRow), Cells(i, 1))
    
    'if it appears in the range more than 1 time...
    If x > 1 Then
        'add the value of the cell and the number of occurances to a string
        
        'if the string value is empty
        If temp = "" Then
            'just add the value/# of occur.
            temp = Cells(i, 1) & "=" & x
        'if the string value already contains text
        Else
            'check to see if the value of the cell is already in the list
            'if it is *not* in the list...
            If InStr(1, temp, Cells(i, 1)) = 0 Then
                'add the value/# of occur. onto the end of the string
                'each item separated by a comma
                temp = temp & "," & Cells(i, 1) & "=" & x
            End If
        End If
    End If
Next i

'once all of the values have been checked

'if the string variable is empty, no duplicates were found
If temp = "" Then
    'display a 0
    [B1] = 0
'if variable is *not* empty
Else
    'split string variable into array, separated by the commas in the string
    temp = Split(temp, ",")
    'loop through the items in the array
    For i = LBound(temp) To UBound(temp)
        'place the value/# of occur. in column B
        Cells(i + 1, 2) = temp(i)
    Next i
End If

End Sub
 
Upvote 0
Thanks very much for the narrative. I've pasted it into excel, but it's not liking the split near the end - "not defined"??
 
Upvote 0
The formula way is probably better if you're not so confident with VBA
Book3
ABCD
1DataNumber of duplicates
2T1Bc21
3T1Ba11
4T1Ba11
5T1Ad1-
6T2Ea21
7T2Ea21
8T1Ab1-
9T1Ac11
10T1Ba2-
11T1Ac3-
12T2Ea4-
13T2Ec4-
14T1Bb2-
15T1Ab23
16T1Ab23
17T1Ab23
18T1Ab23
19T1Ac11
20T1Bc21
Sheet1


Hope it helps
 
Upvote 0
Hi Tim

Here is another solution. Gets the input from A2 down and writes the results in columns C:D row 2 down.

Code:
Sub MoreThanOnce()
Dim rCell As Range, rRng As Range, vKey, lrow As Long

Set rRng = Range("A2", Range("A" & Rows.Count).End(xlUp))

With CreateObject("Scripting.dictionary")
    .comparemode = vbTextCompare
    
    ' load the info
    For Each rCell In rRng
        If Not .exists(rCell.Value) Then _
            .Add rCell.Value, Application.WorksheetFunction.CountIf(rRng, rCell.Value)
    Next rCell

    ' Write the result in columns C:D
    lrow = 2
    For Each vKey In .keys
        If .Item(vKey) > 1 Then
            Cells(lrow, "C") = vKey
            Cells(lrow, "D") = .Item(vKey) - 1
            lrow = lrow + 1
        End If
    Next vKey
End With
End Sub

Hope this helps
PGC
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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