0

I found the index of missing arrays using the following function in Excel VBA:

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

How do I extract the values that are missing in array "Data" when compared with the reference array "varrayReference"?

I can seen the non-matching arrays in the array "absentArray" but I need the corresponding missing IDs that are in the "Data" array (for example Data(18), Data(20) that show non matches in the "'absentArray"). Is there a way to get the missing cells in the "Data" array? Thanks!

enter image description here

enter image description here

1
  • Why not do the reverse match and store the results in another array or a range? Commented Mar 11, 2019 at 19:37

1 Answer 1

1

Don't use VBA'a Filter if you want a 1:1 match; Filter acts like a wildcard search so positive returns would be found for 123 in qw123op and df123uy.

Using the worksheet's Match function quickly locates 1:1 matches but are not case sensitive.

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a bunch. I am trying to just get unique values in the array missingData. Any inputs?
Switch the coding to add missing items into a dictionary as the dictionary's keys if you want uniqueness.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.