If UBound(Filter(myArray, Sheets(i).Cells(1, j).Value, True)) = -1 Then
'take action
End if
我使用此语法将在Cells(1,j)中找到的元素(例如“ ally”)与数组的所有元素(例如“ mally”,“ kate”,“ becks”)进行比较,并在没有找到完全匹配的内容。
麻烦的是,根据这一行代码,似乎“盟友”被视为与“盟友”匹配(可能是因为“盟友”是“盟友”的子字符串),而我希望“盟友”被识别为与“盟友”不同”。
对实现这一点的语法有帮助吗?谢谢!
最佳答案
过滤器将返回部分匹配的所有项目。 Microsoft建议的解决方法是,然后在过滤后的数组中搜索精确匹配。
Function FilterExactMatch(astrItems() As String, _
strSearch As String) As String()
' This function searches a string array for elements
' that exactly match the search string.
Dim astrFilter() As String
Dim astrTemp() As String
Dim lngUpper As Long
Dim lngLower As Long
Dim lngIndex As Long
Dim lngCount As Long
' Filter array for search string.
astrFilter = Filter(astrItems, strSearch)
' Store upper and lower bounds of resulting array.
lngUpper = UBound(astrFilter)
lngLower = LBound(astrFilter)
' Resize temporary array to be same size.
ReDim astrTemp(lngLower To lngUpper)
' Loop through each element in filtered array.
For lngIndex = lngLower To lngUpper
' Check that element matches search string exactly.
If astrFilter(lngIndex) = strSearch Then
' Store elements that match exactly in another array.
astrTemp(lngCount) = strSearch
lngCount = lngCount + 1
End If
Next lngIndex
' Resize array containing exact matches.
ReDim Preserve astrTemp(lngLower To lngCount - 1)
' Return array containing exact matches.
FilterExactMatch = astrTemp
End Function
此代码取自http://msdn.microsoft.com/en-us/library/office/aa164525%28v=office.10%29.aspx
关于vba - VBA阵列-检查严格(非近似)匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14205999/