Excel VBA to Identify Duplicates from one Worksheet Based on Cell Value from Another Worksheet

rarascon

New Member
Joined
Mar 22, 2012
Messages
21
Hi there:

I'm seeing some similar requests and code, and I've been trying to accomplish what I need with either one. However, I'm not quite there or get errors.

My apologies that my code below is a skeleton and not organized very well. I was trying a couple of options, and I've not added any error handling or the usual 'with application' blocks yet.
I'm hoping to use the selected cell value from SourceSht and evaluate for dupes from the table from DID_Employee sheet. When it does find dupes, the user will see a message using the information below alerting them that there are dupes and to select the correct EEID. There may be a better solution for that as well.

I appreciate any assistance!
Rob R

VBA Code:
Private Sub TestForDuplicates()
'Determine if duplicate values exist in a provided range and alert user to select the correct EEID

Dim rng1 As Range, frng As Range
Dim myArray As Variant
Dim x As Integer
Dim SourceSht As Worksheet, ws2 As Worksheet
Dim lR1 As Integer, lR2 As Integer

Set SourceSht = ThisWorkbook.Worksheets("Exceptions_HRCases") 'Sheet with source value
Set ws2 = ThisWorkbook.Worksheets("DID_Employee") 'Sheet to find dupes
'Set ws2 = ActiveSheet

    With SourceSht
        .Range("E2").End(xlDown).Select
    End With
        
    With ws2
        lR2 = .Cells(.Rows.Count, 2).End(xlUp).Row
    End With

Set rng1 = SourceSht.Selection

'Use UNIQUE Function and store result to an Array variable
    myArray = WorksheetFunction.Unique(rng1)
    
'Test Range for Duplicates with COUNTIFS Function  *** THIS DOES NOT WORK but I would like to use something similar using the array from ws2.  ***
  For x = LBound(myArray) To UBound(myArray)
    If WorksheetFunction.countifs(rng1, myArray(x,1))> 1 Then
      MsgBox "Found more than one value of " & Chr(34) & myArray(1, x) & Chr(34) & " in the cell range. Please confirm EEID."
      Exit Sub
    End If
  Next x

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try a formula in the "Exceptions_HRCases" sheet

Dante Amor
AEFG
1VALUESDups
2Value1 
3Value2Found more than one value
4Value3 
5Value4Found more than one value
6Value5 
7Value6Found more than one value
Exceptions_HRCases
Cell Formulas
RangeFormula
G2:G7G2=IF(COUNTIF(DID_Employee!B:B,Exceptions_HRCases!E2)>1,"Found more than one value","")


Dante Amor
AB
1Values
2Value1
3Value2
4Value3
5Value4
6Value5
7Value6
8Value2
9Value4
10Value6
DID_Employee
 
Upvote 0
Solution
Try a formula in the "Exceptions_HRCases" sheet

Dante Amor
AEFG
1VALUESDups
2Value1 
3Value2Found more than one value
4Value3 
5Value4Found more than one value
6Value5 
7Value6Found more than one value
Exceptions_HRCases
Cell Formulas
RangeFormula
G2:G7G2=IF(COUNTIF(DID_Employee!B:B,Exceptions_HRCases!E2)>1,"Found more than one value","")


Dante Amor
AB
1Values
2Value1
3Value2
4Value3
5Value4
6Value5
7Value6
8Value2
9Value4
10Value6
DID_Employee
Thank you, Dante! That would be the easier option as I have no problem setting up formulas; however, in this case this is a front-end user form where they need to be alerted when there are more than one Employee with the same name for the name that they are entering. I'm looking for an automated solution. = )
 
Upvote 0
Try a formula in the "Exceptions_HRCases" sheet

Hey, @DanteAmor, I'm flexible! So, I used your solution and it's working now. I added some helper columns in the workbook and added this code with the other Worksheet_Change automated tasks. Works great now. Thank you!
VBA Code:
    If Target.Offset(0, xOffsetColumn2).Value > "" Then
    Debug.Print Target.Value
        MsgBox "There is more than one value of " & Chr(34) & Target.Value & Chr(34) & " in the Employee table. Please confirm EEID and CC."

    End If
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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