用于检查列中重复项的宏
Macro to check for duplicates in a column
我浏览了该站点并找到了几个查找重复项的代码示例,但它们仅匹配部分大小写而不是完全匹配。
我有一个宏,它接受一个单元格值,然后在列中查找任何重复项并为它找到的任何重复项增加计数。但是,它可以找到部分匹配的重复项,但我需要它只匹配完全匹配的重复项。
例如,目前如果我有一行包含 1,另一行包含 11,它会将它们突出显示为重复行。
这是我目前拥有的代码。
Function CountMatches(searchvalue As String, sheet As Worksheet, r As String) As Integer
Dim firstFound As Range
Dim lastFound As Range
Dim matchCount As Integer
Set firstFound = sheet.Range(r).Find(searchvalue, After:=ActiveCell, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=True)
sheet.Range(r).Select
Set firstFound = sheet.Range(r).Find(What:=searchvalue, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
If firstFound Is Nothing Then
CountMatches = 0
Else
Do
Set lastFound = sheet.Range(r).Find(What:=searchvalue, After:=IIf(lastFound Is Nothing, firstFound, lastFound), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
matchCount = matchCount + 1
Loop Until lastFound Is Nothing Or firstFound.Address = lastFound.Address
CountMatches = matchCount
End If
End Function
您需要查看整个单元格的内容而不是其中的一部分,即将 LookAt:=xlPart
更改为 LookAt:=xlWhole
。
我浏览了该站点并找到了几个查找重复项的代码示例,但它们仅匹配部分大小写而不是完全匹配。
我有一个宏,它接受一个单元格值,然后在列中查找任何重复项并为它找到的任何重复项增加计数。但是,它可以找到部分匹配的重复项,但我需要它只匹配完全匹配的重复项。
例如,目前如果我有一行包含 1,另一行包含 11,它会将它们突出显示为重复行。
这是我目前拥有的代码。
Function CountMatches(searchvalue As String, sheet As Worksheet, r As String) As Integer
Dim firstFound As Range
Dim lastFound As Range
Dim matchCount As Integer
Set firstFound = sheet.Range(r).Find(searchvalue, After:=ActiveCell, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=True)
sheet.Range(r).Select
Set firstFound = sheet.Range(r).Find(What:=searchvalue, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
If firstFound Is Nothing Then
CountMatches = 0
Else
Do
Set lastFound = sheet.Range(r).Find(What:=searchvalue, After:=IIf(lastFound Is Nothing, firstFound, lastFound), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
matchCount = matchCount + 1
Loop Until lastFound Is Nothing Or firstFound.Address = lastFound.Address
CountMatches = matchCount
End If
End Function
您需要查看整个单元格的内容而不是其中的一部分,即将 LookAt:=xlPart
更改为 LookAt:=xlWhole
。