MossyPants
New Member
- Joined
- May 21, 2022
- Messages
- 23
- Office Version
- 365
- Platform
- Windows
I have a table where I would like rows to get highlighted if data in columns B:H is duplicated.
This the code I have so far:
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!!
Book1.xlsm | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | Condition | Treatment | Dose | Lot number | Date | |||
2 | A | sweet | none | Abc | 10-Jan | |||
3 | B | sweet | 10 | Abc | 10-Jan | |||
4 | C | sour | 0 | Abc | 11-Jan | |||
5 | D | sour | Abc | 11-Jan | ||||
6 | E | sour | Abc | 12-Jan | ||||
7 | F | sweet | 10 | Abc | 10-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!!