在Excel VBA中使用Application.Match在两个数组中查找非匹配字符串

时间:2019-03-11 19:33:13

标签: excel vba

我在Excel VBA中使用以下函数找到了丢失数组的索引:

absentArr = Application.Match(Data, varrayReference, False)

与参考数组“ varrayReference”相比,如何提取数组“数据”中缺少的值?

我可以看到数组“ absentArray”中不匹配的数组,但是我需要“ Data”数组中相应的缺失ID(例如,Data(18),Data(20)在“'absentArray”)。有没有办法获取“数据”数组中缺少的单元格?谢谢!

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您想进行1:1匹配,请不要使用VBA'a过滤器;过滤器的作用类似于通配符搜索,因此会在 qw123op df123uy 中找到 123 的正收益。

使用工作表的“匹配”功能可以快速找到1:1匹配项,但不区分大小写。

Sub findMissing()

    Dim i As Long, j As Long
    Dim data As Variant, absentArray As Variant, missingData As Variant
    Dim dict as object, k as variant

    Set dict = CreateObject("scripting.dictionary")
    dict.CompareMode = vbTextCompare

    data = Array("AB", "BC", "CD", "DE", "EF", "FG", "GH")
    absentArray = Array("AB", "BC", "DE", "GH")
    ReDim missingData(LBound(data) To UBound(data))

    j = LBound(data)
    For i = LBound(data) To UBound(data)
        If IsError(Application.Match(data(i), absentArray, 0)) Then
            'method one with array
            missingData(j) = data(i)
            j = j + 1
            'method two with dictionary
            dict.Item(data(i)) = vbNullString
        End If
    Next i
    ReDim Preserve missingData(j - 1)

    For i = LBound(missingData) To UBound(missingData)
        Debug.Print missingData(i)
    Next i

    For Each k in dict.Keys
        Debug.Print k
    Next k

End Sub