Q:

Remove Listbox item if match not found in VBA

I am writing vba code that will search all listbox items against a entire column in a sheet.
If listbox item not found in Excel sheet column, i want to delete the item from list. I tried few codes, its showing some error as "Could not get list property, Invalid property array index". Below is my code i am using currently.
Private Sub CommandButton1_Click()
    Dim itemExistResults As Boolean
    Dim myarray()
    Dim intItem As Long

    myarray = Application.Transpose(Sheet1.Range("a2:a1000"))

    For intItem = 0 To ListBox1.ListCount - 1
        If IsInArray(ListBox1.List(intItem), myarray) Then
        Else
            ListBox1.RemoveItem intItem
        End If
    Next

End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = UBound(Filter(arr, stringToBeFound)) > -1
End Function

Any idea where i am wrong in this code.

A:

You should iterate from the last item of list to the first, because removing items changes their indexation.
Try to change your loop like that:
    For intItem = ListBox1.ListCount - 1 To 0 Step -1
        If IsInArray(ListBox1.List(intItem), myarray) Then
        Else
            ListBox1.RemoveItem intItem
        End If
    Next

I have a tip for you connected with your task, but not exactly with the error described in question.
For this type of task you should use object of Dictionary type instead of iterating through array - it would be much more effective. 
I have modified your code to use dictionary. Check it and compare the time each of those solutions need to complete this task - the one with dictionary should be much faster. If you have any questions regarding this code, let me know in comments.
Private Sub CommandButton1_Click()
    Dim myArray As Variant
    Dim intItem As Long
    Dim dict As Object
    Dim i As Long
    Dim value As Variant

    '--- [Loading data into dictionary] ------------------------------------
    Set dict = VBA.CreateObject("Scripting.Dictionary")
    myArray = Sheet1.Range("A2:A1000")

    'Iterate through all the items in array and load them into dictionary.
    For i = LBound(myArray) To UBound(myArray)

        value = myArray(i, 1)

        If Not IsEmpty(value) Then
            If Not dict.exists(value) Then
                Call dict.Add(value, 0)
            End If
        End If
    Next i
    '-----------------------------------------------------------------------

    '--- [Comparing ListBox with dictionary] -------------------------------
    With ListBox1
        For intItem = .ListCount - 1 To 0 Step -1
            value = .List(intItem)

            If Not dict.exists(value) Then
                .RemoveItem intItem
            End If

        Next
    End With
    '-----------------------------------------------------------------------

End Sub

