Fishboy
Well-known Member
- Joined
- Feb 13, 2015
- Messages
- 4,267
Hi all,
I am working on an Excel 2010 workbook and have implemented the following tweaked code I found online. In essence it is a worksheet change event which highlights cells red when duplicate values are added to column A (which is a list of candidate names).
This code is working fine as is, however I need to expand upon the scope it is using to determine if this is a duplicate or not. My data is laid out as per the example below:
Excel 2010
<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: #403151, align: center"]Candidate
Name[/TD]
[TD="bgcolor: #403151, align: center"]Contact
Number[/TD]
[TD="bgcolor: #403151, align: center"]Email
Address[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]Piotr Pagowski[/TD]
[TD="align: center"]07749 xxxxxx[/TD]
[TD="align: center"]p.pagowski@xxxxxx.com[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]David Lewis[/TD]
[TD="align: center"]07967 xxxxxx[/TD]
[TD="align: center"]dave135400@xxxxxx.com[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]Piotr Klimecki[/TD]
[TD="align: center"]07511 xxxxxx[/TD]
[TD="align: center"]piter62@xxxxxx.pl[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]Andrew Flower[/TD]
[TD="align: center"]01992 xxxxxx[/TD]
[TD="align: center"]andrew.flower33@xxxxxx.com[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]Benjamin Matthews[/TD]
[TD="align: center"]07973 xxxxxx[/TD]
[TD="align: center"]bmatthews@xxxxxx.com[/TD]
[TD="align: center"]7[/TD]
[TD="align: center"]Trevor Campbell[/TD]
[TD="align: center"]07546 xxxxxx[/TD]
[TD="align: center"]trevorcampbell13@xxxxxx.co.uk[/TD]
</tbody>
I realise there are no duplicates in my example, but it just to demonstrate how the data is laid out. Ultimately I need to tweak the code so that it checks columns A:C and only highlights the cells if ALL THREE columns match another entry. There are a whole load of other columns of data after column C, but it is the name, contact number and email address we will use to identify duplicates.
Can anyone help at all?
I am working on an Excel 2010 workbook and have implemented the following tweaked code I found online. In essence it is a worksheet change event which highlights cells red when duplicate values are added to column A (which is a list of candidate names).
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 Then Exit Sub ' IF ITS A HEADER, DO NOTHING.
On Error GoTo ErrHandler
Application.ScreenUpdating = False
Dim myDataRng As Range
Dim cell As Range
' WE WILL SET THE RANGE (SECOND COLUMN).
Set myDataRng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
For Each cell In myDataRng
cell.Offset(0, 0).Font.Color = vbBlack ' DEFAULT COLOR.
' LOCATE DUPLICATE VALUE(S) IN THE SPECIFIED RANGE OF DATA.
If Application.Evaluate("COUNTIF(" & myDataRng.Address & "," & cell.Address & ")") > 1 Then
cell.Offset(0, 0).Interior.Color = vbRed ' CHANGE CELL COLOR TO RED.
End If
Next cell
Set myDataRng = Nothing
ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This code is working fine as is, however I need to expand upon the scope it is using to determine if this is a duplicate or not. My data is laid out as per the example below:
Excel 2010
A | B | C | |
---|---|---|---|
<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: #403151, align: center"]Candidate
Name[/TD]
[TD="bgcolor: #403151, align: center"]Contact
Number[/TD]
[TD="bgcolor: #403151, align: center"]Email
Address[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]Piotr Pagowski[/TD]
[TD="align: center"]07749 xxxxxx[/TD]
[TD="align: center"]p.pagowski@xxxxxx.com[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]David Lewis[/TD]
[TD="align: center"]07967 xxxxxx[/TD]
[TD="align: center"]dave135400@xxxxxx.com[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]Piotr Klimecki[/TD]
[TD="align: center"]07511 xxxxxx[/TD]
[TD="align: center"]piter62@xxxxxx.pl[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]Andrew Flower[/TD]
[TD="align: center"]01992 xxxxxx[/TD]
[TD="align: center"]andrew.flower33@xxxxxx.com[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]Benjamin Matthews[/TD]
[TD="align: center"]07973 xxxxxx[/TD]
[TD="align: center"]bmatthews@xxxxxx.com[/TD]
[TD="align: center"]7[/TD]
[TD="align: center"]Trevor Campbell[/TD]
[TD="align: center"]07546 xxxxxx[/TD]
[TD="align: center"]trevorcampbell13@xxxxxx.co.uk[/TD]
</tbody>
Sheet1
I realise there are no duplicates in my example, but it just to demonstrate how the data is laid out. Ultimately I need to tweak the code so that it checks columns A:C and only highlights the cells if ALL THREE columns match another entry. There are a whole load of other columns of data after column C, but it is the name, contact number and email address we will use to identify duplicates.
Can anyone help at all?