【发布时间】:2019-07-27 00:25:56
【问题描述】:
想要在 excel 的列中查找重复项,并希望在找到 1 个重复项时弹出一个 msgbox,如果找到多个重复项,它不应该继续弹出消息。
另外,如果我可以使用两个列单元格值并一起使用它来查找重复项,这也会很有帮助。
Sub ColumnDuplicates()
Dim lastRow As Long
Dim matchFoundIndex As Long
Dim iCntr As Long
lastRow = Range("A65000").End(xlUp).Row
For iCntr = 1 To lastRow
If Cells(iCntr, 1) <> "" Then
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)
If iCntr <> matchFoundIndex Then
MsgBox ("There are duplicates in Column A")
End If
End If
Next
MsgBox ("No Duplicates in Column A")
End Sub
期望打印消息说 A 列有重复项或没有重复项
【问题讨论】:
-
不是直接重复,但this answer 谈论在列中查找重复
-
@calestini:我喜欢 OP 在该链接中采用的方法
.RemoveDuplicates。我会继续采用这种方法,因为它不涉及任何循环。但是,我将使用临时工作表,而不是在同一张工作表中使用列。 -
但这也取决于用户是否使用 Excel 2007+... 查看
"A65000",用户可能仍在使用 Excel 2003。 -
您使用的是哪个 Excel 版本?
-
@MathieuGuindon 我同意你的看法 :) 我已经在 post