Worksheet change event to remove duplicate values on multiple sheets

Matei

New Member
Joined
Apr 2, 2012
Messages
24
I’m trying to initiate a worksheet change event which will find duplicate values across multiple worksheets.
So if I enter a number in Sheet1 which is already in Sheet2, I’ll get a message informing me and then delete the number which I just entered.
(and vise versa if I enter a number of Sheet2, it will check Sheet1 for duplicate)

I’ve made the below, but the problem is it searches from left-top to right-bottom so if I enter a number further down the list, it deletes the existing number – not the new one.
I just need something to delete the new number.


The numbers are in A2:N36 of both sheets.
On each sheet I have a Worksheet_SelectionChange to call a separate macro “DuplicateValue”
This is the macro to check for duplicates.

Code:
Sub DuplicateValue()
Dim Values, Values2 As Range, Cell
 
'set worksheets and ranges to cover where the numbers are entered
    Set Values = Worksheets("Sheet1").Range("A1:N36")
    Set Values2 = Worksheets("Sheet2").Range("A1:N36")
 
'check for duplicates on first worksheet
    For Each Cell In Values
        If WorksheetFunction.CountIf(Values, Cell.Value) > 1 Then
            MsgBox Cell & " is already appearing!!"
            Cell.ClearContents
        End If
    Next Cell
 
'check for duplicates on second worksheet
    For Each Cell In Values2
        If WorksheetFunction.CountIf(Values2, Cell.Value) > 1 Then
            MsgBox Cell & " is already appearing!!"
            Cell.ClearContents
        End If
    Next Cell
 
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try placing this in to the worksheet_change event in Sheet1 (and swap the sheet reference for Sheet2)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    If Not Intersect(Target, Range("$A$2:$N$36")) Is Nothing Then
        
        If WorksheetFunction.CountIf(Sheets("Sheet2").Range("A2:N36"), Target.Value) > 0 Then
        
            MsgBox Target.Value & " already exists"
            Target.Cells.ClearContents
            
        End If
        
    End If


End Sub

Hope this helps

Edit: It has just occured to me that you probably meant to delete if it was a duplicate in any sheet. If so try this in Sheet1 (and edit for Sheet2)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("$A$2:$N$36")) Is Nothing Then
        
        If WorksheetFunction.CountIf(Sheets("Sheet2").Range("A2:N36"), Target.Value) > 0 Or _
           WorksheetFunction.CountIf(Sheets("Sheet1").Range("A2:N36"), Target.Value) > 1 Then
           
            MsgBox Target.Value & " already exists"
            Target.Cells.ClearContents
            
        End If
        
    End If


End Sub
 
Last edited:
Upvote 0
Hi S.ridd,

Yes, the second one is what I need and it works perfectly - exactly what I need.

Thanks :D
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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