【问题标题】:Remove duplicates and alert user with VBA使用 VBA 删除重复项并提醒用户
【发布时间】:2023-04-11 08:10:01
【问题描述】:

我是使用肥皂调用的重要数据,我的问题是在某些情况下我可能会得到重复的数据。

我可以使用
WS.Range("A6:O200").RemoveDuplicates Columns:=(2)

轻松删除这些数据

但是,我希望在发生这种情况时通过 MsgBox 提醒用户。目前,我正在尝试使用从此处的另一篇文章改编的一些代码来实现此功能。

Dim dict As Object

' Let Col be the column which warnDupes operates on.
Dim Col As String

Col = "B"

Set dict = CreateObject("scripting.dictionary")

dupeRow = Range(Col & Rows.Count).End(xlUp).Row

On Error Resume Next
For i = dupeRow To 1 Step -1
    If dict.Exists(UCase$(Range(Col & i).Value)) = True Then

    'range("Y" & i).EntireRow.Delete
    WS.Range("A6:O200").RemoveDuplicates Columns:=(2)

    'MsgBox ("Hmm...Seems to be a duplicate of " & Range(Col & i).Value & _
    " in Cell " & Col & i)

End If

dict.Add UCase$(Range(Col & i).Value), 1
Next

 MsgBox ("Duplicate unfullfilled requests where removed")

问题当然是,这要么显示循环中删除的每个重复值的消息,要么即使没有重复值(就像现在一样)。理想情况下,我想要的是删除重复项以完全运行,然后通过消息提醒用户。

问候 山姆

【问题讨论】:

  • 您可以设置字符串形式的计数器。用它来连接你的循环而不是 Msgbox。循环结束后,您可以打印连接的文本作为警告。

标签: excel vba duplicates


【解决方案1】:
Dim dict As Object

' Let Col be the column which warnDupes operates on.
Dim Col As String
Dim bCount as Boolean

Col = "B"

Set dict = CreateObject("scripting.dictionary")

dupeRow = Range(Col & Rows.Count).End(xlUp).Row

On Error Resume Next
For i = dupeRow To 1 Step -1
    If dict.Exists(UCase$(Range(Col & i).Value)) = True Then

        'range("Y" & i).EntireRow.Delete
        WS.Range("A6:O200").RemoveDuplicates Columns:=(2)

        bCount = True

        'MsgBox ("Hmm...Seems to be a duplicate of " & Range(Col & i).Value & _
        " in Cell " & Col & i)

    End If

    dict.Add UCase$(Range(Col & i).Value), 1
Next

If bCount Then
    MsgBox ("Duplicate unfullfilled requests where removed")
End If

【讨论】:

  • 嗨,Michal,感谢您提供此代码。对我来说,有些东西仍然不太合适。它总是触发消息。通过更新消息框的代码,我可以看到它认为单元格 B10 中存在重复值 1234。 B10 恰好是我包含该值的最后一行数据
  • 我设法通过将我的 dupeRow 设置为 6 来解决这个问题(这就是我的数据开始的地方)。感谢您的帮助
猜你喜欢
  • 2017-05-03
  • 2021-07-16
  • 2021-11-27
  • 2021-06-19
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多