Find x number of duplicates and highlight duplicates rows

DBjjprint

New Member
Joined
May 8, 2024
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Need a macro to find greater the 5 duplicates in a target field and highlight rows or just the target field
I am doing this manually with Countif and Conditional formatting rules but I can not get a macro to work
Duplicates are always consecutive
This is the data
plus 5 dupes.xlsx
ABC
1FIRST_NAMELAST_NAMEACCOUNT_NBR
2123
3123
4123
512
612
7123456
8123456
9123456
10123456
11123456
12123456
131234
141234
151234
161234
1712345678
1812345678
1912345678
2012345678
2112345678
2212345678
2312345678
2412345678
Export


This is the needed results
plus 5 dupes.xlsx
ABC
1FIRST_NAMELAST_NAMEACCOUNT_NBR
2123
3123
4123
512
612
7123456
8123456
9123456
10123456
11123456
12123456
131234
141234
151234
161234
1712345678
1812345678
1912345678
2012345678
2112345678
2212345678
2312345678
2412345678
Export
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try:

VBA Code:
Sub FiveInAColumn()
Dim MyRange As Range, OutRange As Range
Dim d As Variant, r As Long, i As Long, NumRows As Long

    NumRows = 5
    Set MyRange = Range("C2:C24")
    Set OutRange = Nothing
    
    d = MyRange.Value
    For r = 1 To UBound(d) - NumRows + 1
        For i = 1 To NumRows - 1
            If d(r, 1) <> d(r + i, 1) Then GoTo Nope:
        Next i
            
        If OutRange Is Nothing Then
            Set OutRange = MyRange.Offset(r - 1).Resize(NumRows)
        Else
            Set OutRange = Union(OutRange, MyRange.Offset(r - 1).Resize(NumRows))
        End If
Nope:
    Next r
    
    OutRange.Interior.Color = vbYellow
            
End Sub
 
Upvote 0
Solution
Thanks for the quick response
This works perfect, just made a couple of modification
Change NumRows to 6, needed to highlight greater than 5, but the code makes it easy to adjust to highlight whatever number of duplicates needed
Then the Range set to C2:C3000 to cover 150 excel files I have to apply this to
Thank you
 
Upvote 0
I need a modification on the above code, need to delete all the other rows that are not highlighted duplicates
Not sure why i didn't think of this before so I don't have to search rows of data to find highlight cells
 
Upvote 0
This should work. Test it on a COPY of your data. It will delete rows and there will be no way to undo it.

VBA Code:
Sub FiveInAColumn()
Dim MyRange As Range, OutRange As Range, DelRange As Range
Dim d As Variant, r As Long, i As Long, NumRows As Long, c As Variant

    NumRows = 5
    Set MyRange = Range("C2:C24")
    Set OutRange = Nothing
    Set DelRange = Nothing
    
    d = MyRange.Value
    For r = 1 To UBound(d) - NumRows + 1
        For i = 1 To NumRows - 1
            If d(r, 1) <> d(r + i, 1) Then GoTo Nope:
        Next i
            
        If OutRange Is Nothing Then
            Set OutRange = MyRange.Offset(r - 1).Resize(NumRows)
        Else
            Set OutRange = Union(OutRange, MyRange.Offset(r - 1).Resize(NumRows))
        End If
Nope:
    Next r
    
    OutRange.Interior.Color = vbYellow

    For Each c In MyRange
        If Intersect(c, OutRange) Is Nothing Then
            If DelRange Is Nothing Then
                Set DelRange = c.EntireRow
            Else
                Set DelRange = Union(DelRange, c.EntireRow)
            End If
        End If
    Next c
    
    DelRange.Delete shift:=xlUp
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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