【问题标题】:How to Check for Duplicates and Display a Count MsgBox如何检查重复并显示计数 MsgBox
【发布时间】:2020-06-06 07:43:36
【问题描述】:

我有三个工作表,基本上我想在工作表 2 的 A 列中选择一个单元格(作为活动单元格)并检查工作表 3 的 A 列中是否有任何重复项(此工作表的范围应该是A1 到最后一行数据)。

如果有任何重复,我想要一个 msgbox 来显示重复值的数量(如果它大于 3)。

我已经添加了 cmets 来解释我在每个步骤中的逻辑,请随时简化我的代码:

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

目前,我的代码到达第一个 IF 语句 并简单地转到 End IF,因此它不会执行超过这一行而只是转到 Next 和然后结束子: If Cell.Value = sh2.Range("A" & rowAC) .Value Then

交叉引用: https://www.mrexcel.com/board/threads/how-to-check-for-duplicates-and-display-a-count-msgbox.1125070/

【问题讨论】:

  • Set Source = sh3.Range("A1").End(xlDown) 仅将一个单元格设置为范围。 A列中第一个空白单元格之前的单元格。我想你想要Set Source = sh3.Range("A1",sh3.Range("A1").End(xlDown))
  • @ScottCraner 我删除了空格(我不知道它是如何到达那里的,我的原始代码中没有空格)。其次,我相信你是对的,在它只是循环通过 A 列中的一个单元格之前。当用你的建议单步执行代码时,我在第一个空白之前遍历整个 A 列,但它永远不会超过第一个 IF 语句仍然
  • 然后查看数据。手动检查数据是否相同,没有多余的空格或其他不可打印的字符并且大小写匹配。由于某种原因,Excel 没有看到匹配项。
  • COUNTIF 的第一个标准是一个范围,然后第二个是一个值 sh3.Range("Source,Cell") 不是正确的范围引用。你可能只想要Source
  • 只是Source 它已经是一个范围。

标签: excel vba duplicates counter


【解决方案1】:

这是我为使用此问题作为其问题参考的任何人使用的最终代码:

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

【讨论】:

    猜你喜欢
    • 2022-10-02
    • 1970-01-01
    • 2017-07-27
    • 2019-12-05
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多