0
votes

I have Three worksheets, and essentially I want to select a cell in Column A of Sheet 2 (As the Active Cell) and check if there are any duplicates in Column A of Sheet 3 (The Range for this Sheet should be from A1 to the last row of Data).

If there are any duplicates, I would like a msgbox to display the number of duplicate values if it's greater than 3.

I have added comments explaining my logic in each step, please feel free to simplify my code as well:

Sub Check_Duplicates()


    'Declaring variables
    Dim Cell As Variant
    Dim Source As Range
    Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
    Dim rowAC As Long
    Dim Counter As Long

    'Assigning a worksheet to the decalred variables
    Set sh1 = Sheet1
    Set sh2 = Sheet2
    Set sh3 = Sheet3

    'Sets the Long variable as the Active Cell Row in Sheet 2
    rowAC = ActiveCell.Row

    'Initializing "Source" variable range to last row in Sheet 3
    Set Source = sh3.Range("A1", sh3.Range("A1").End(xlDown)) 

    'Looping through each cell in the "Source" variable Range
    For Each Cell In Source

        'Checking if the "Cell" values in Sheet 3 (in column A to the last row) are equal to the value in the Active Cell in Column A
        If Cell.Value = sh2.Range("A" & rowAC).Value Then

            'Checking whether the value in "Cell" already exists in the "Source" range
            If Application.WorksheetFunction.CountIf(Source, Cell) > 1 Then

                'Counts and stores the number of duplicate values from Sheet 3 "Cells" compared to the Active Cell value in Sheet 1 Column A
                Counter = Application.WorksheetFunction.CountIf(sh3.Range("Source,Cell"), sh2.Range("A" & rowAC))

                'If there are more than 3 duplicates then display a message box
                If Counter > 3 Then

                    'Msgbox displaying the number of duplicate values in Sheet 3
                    MsgBox "No. of duplicates is:" & Counter

                End If

            End If

        End If

    Next

End Sub

Currently, my code gets to the first IF Statement and simply goes to the End IF, so it doesn't execute past this line and simply goes to Next and then End Sub: If Cell.Value = sh2.Range("A" & rowAC) .Value Then

Cross Referencing: https://www.mrexcel.com/board/threads/how-to-check-for-duplicates-and-display-a-count-msgbox.1125070/

1
Set Source = sh3.Range("A1").End(xlDown) is only setting one cell to the range. The cell right before the first blank cell in Column A. I think you want Set Source = sh3.Range("A1",sh3.Range("A1").End(xlDown))Scott Craner
@ScottCraner I removed the space (I don't know how it got there, there's no space in my original code). Secondly, I believe you're right, before it was just looping through one cell in column A. When stepping through the code with your suggestion, I loop through the entirety of Column A right before the first blank, but it's never going past the first IF statement stillHandreen
then look at the data. manually check that the data is the same, that there are no extra spaces or other non printable characters and the case matches. For some reason Excel is not seeing a match.Scott Craner
the first criterion of COUNTIF is a range then the second is a value sh3.Range("Source,Cell") is not proper range reference. You probably want just SourceScott Craner
Just Source it already is a range.Scott Craner

1 Answers

0
votes

Here is the final code I am using for anyone using this question as reference for their issues:

Sub Check_Duplicates()
    'Declaring variables
    Dim Source As Range
    Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
    Dim rowAC As Long, Counter As Long

    'Assigning a worksheet to the decalred variables
    Set sh1 = Sheet1
    Set sh2 = Sheet2
    Set sh3 = Sheet3

    'Sets the Long variable as the Active Cell Row in Sheet 2
    rowAC = ActiveCell.Row

    'Initializing "Source" variable range to last row in Sheet 3
    Set Source = sh3.Range("A1", sh3.Range("A" & Rows.Count).End(xlUp))

    'count number of times is in Source range
    Counter = Application.WorksheetFunction.CountIf(Source, sh2.Range("A" & rowAC))

    'If there are more than 3 duplicates then display a message box
    If Counter > 3 Then
        'Msgbox displaying the number of duplicate values in Sheet 3
        MsgBox "No. of duplicates is: " & Counter
    End If
End Sub