Duplicate values across multiple sheets in an excel workbook

khajduk

New Member
Joined
Dec 22, 2021
Messages
1
Office Version
  1. 365
Hi, I am using the code below to attempted to highlight any duplicate phone numbers that would be in column C across multiple sheets in a workbook, however, the code I am using is only highlighting the duplicate number within that particular sheet. Any help would be greatly appreciated.

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Row = 1 Then Exit Sub

On Error GoTo ErrHandler

Application.ScreenUpdating = False

Dim myDataRng As Range

Dim cell As Range

Set myDataRng = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)

For Each cell In myDataRng

cell.Offset(0, 0).Font.Color = vbBlack

If Application.Evaluate("COUNTIF(" & myDataRng.Address & "," & cell.Address & ")") > 1 Then

cell.Offset(0, 0).Font.Color = vbRed

End If

Next cell

Set myDataRng = Nothing

ErrHandler:

Application.EnableEvents = True

Application.ScreenUpdating = True

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Do you want to check all sheets at once or as the values are entered into cells in column C

The script you have checks the active sheet as the values are entered not all the sheets at the same time.

And when you say multiple sheets do you mean all sheets if not what are the names of the sheets.

This script will work on all sheets as you enter the values in each sheet.

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Double click on This Workbook

Paste the code in the VBA edit window

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Modified  12/23/2021  12:48:12 AM  EST
If Target.Row = 1 Then Exit Sub

On Error GoTo ErrHandler

Application.ScreenUpdating = False

Dim myDataRng As Range

Dim cell As Range

Set myDataRng = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)

For Each cell In myDataRng

cell.Offset(0, 0).Font.Color = vbBlack

If Application.Evaluate("COUNTIF(" & myDataRng.Address & "," & cell.Address & ")") > 1 Then

cell.Offset(0, 0).Font.Color = vbRed

End If

Next cell

Set myDataRng = Nothing

ErrHandler:

Application.EnableEvents = True

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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