2015-04-20 90 views
1

我查看了網站,發現了幾個代碼示例,查找重複項,但它們只匹配部分大小寫且不完全匹配。宏檢查列中的重複項

我有一個宏,它接受一個單元格值,然後在列中查找任何重複項併爲其找到的任何重複項計數。然而,它的作品,因爲它發現部分匹配重複,但我需要它只匹配完全匹配重複。

例如,如果我有一行包含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 

回答

1

你需要看整個單元格的內容,而不是它的一部分,即改變LookAt:=xlPartLookAt:=xlWhole

Range.Find documentation