VBA - highlighting rows with duplicate values

MossyPants

New Member
Joined
May 21, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I have a table where I would like rows to get highlighted if data in columns B:H is duplicated.

Book1.xlsm
ABCDEF
1ConditionTreatmentDoseLot numberDate
2AsweetnoneAbc10-Jan
3Bsweet10Abc10-Jan
4Csour0Abc11-Jan
5DsourAbc11-Jan
6EsourAbc12-Jan
7Fsweet10Abc10-Jan
8
Sheet1


This the code I have so far:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lastRow As Integer, rowno As Integer
    Dim isMatched As Boolean
     
    lastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    
    With Sheets("Sheet1")
        For rowno = 2 To lastRow
            .Range(.Cells(rowno, 2), .Cells(rowno, 5)).Interior.Color = xlNone
            For checkRow = 2 To lastRow
                isMatched = (rowno <> checkRow)
                For colno = 2 To 5
                    If checkRow <> rowno And .Cells(checkRow, colno) <> .Cells(rowno, colno) Then
                         isMatched = False
                    End If
                Next
                If isMatched Then
                  .Range(.Cells(rowno, 2), .Cells(rowno, 5)).Interior.Color = RGB(255, 255, 153)
                End If
            Next
        Next
    End With
    
End Sub

It works, with two exceptions.
1) If I enter some values into the table, then delete them entirely, leaving the table blank, the rows remain highlighted.
2) Cells with 0 (zero) and blank cells are marked as duplicates (rows 4&5 above).

Any help would be greatly appreciated!!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Maybe
If isMatched And Cells(checkRow, colno) <> "" And Cells(checkRow, colno) <> 0 Then

Not seeing a need to check both cells since they supposedly match if your current line would be executed.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
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