【问题标题】:VBA Arrays - Check strict (not approximative) matchVBA 数组 - 检查严格(非近似)匹配
【发布时间】:2015-01-09 20:49:45
【问题描述】:
If UBound(Filter(myArray, Sheets(i).Cells(1, j).Value, True)) = -1 Then
 'take action
End if

我使用此语法将 Cells(1, j) 中的一个元素(例如“ally”)与数组的所有元素(例如“mally”、“kate”、“becks”)进行比较,并取未找到完全匹配时的操作。 麻烦的是,根据这行代码,似乎“ally”被认为是匹配“mally”(可能是因为“ally”是“mally”的子字符串),而我希望“ally”被识别为与“mally”不同”。

对实现这一点的语法有任何帮助吗?谢谢!

【问题讨论】:

标签: vba excel-2007


【解决方案1】:
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))

【讨论】:

    【解决方案2】:

    过滤器将返回任何部分匹配的项目。 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

    【讨论】:

      【解决方案3】:

      如果该数组仅用于此比较而不用于其他任何操作,您还可以通过添加您自己的从未出现在数据中的分隔符来强制进行全字比较 - 可能是方括号。
      因此,如果您将数组更改为包含“[mally]”、“[kate]”、“[becks]”
      那么你的情况就变成了:

      If UBound(Filter(myArray, "[" & Sheets(i).Cells(1, j).Value & "]", True)) = -1
      

      【讨论】:

        【解决方案4】:

        如果你不需要使用过滤器,那么下面的 sn-p 就可以了

        Dim v
        Dim bMatch As Boolean
        bMatch = False
        
        For Each v In myArray
            'compare strings
            If StrComp(CStr(v), Sheets(i).Cells(1, j).Value, vbTextCompare) = 0 Then
                bMatch = True
            End If
        Next
        
        If Not bMatch Then
        'do something
        End If
        

        【讨论】:

          猜你喜欢
          • 2012-11-21
          • 1970-01-01
          • 1970-01-01
          • 2015-07-20
          • 1970-01-01
          • 1970-01-01
          • 2011-05-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多