3

This code works fine

Dim MyArray

MyArray = Array("fee", "fi", "fo", "fum")

If Application.Match("fee", MyArray, 0) Then
   Debug.Print "Pass"
End If

However, this code throws a type mismatch error on the if statement

Dim MyArray

MyArray = Array("fee", "fi", "fo", "fum")

If Application.Match("foo", MyArray, 0) Then
   Debug.Print "Pass"
End If

What am I missing?

7
  • 4
    learn.microsoft.com/en-us/office/vba/api/… Match returns the #N/A error if there is no match. VBA is kicking back a type mismatch because an IF statement is looking for a boolean value it can't parse the returned error. Commented Jul 17 at 15:07
  • Ok... I probably learned that years ago and forgot... Thank you Commented Jul 17 at 15:09
  • 4
    If IsNumeric(Application.Match("foo", MyArray, 0)) Then or If Not IsError(Application.Match("foo", MyArray, 0)) Then Commented Jul 17 at 15:20
  • 1
    There is no IsNA in VBA. I prefer IsNumeric in this case, but it's all the same because the result can only be an integer (whole number) or a #N/A error. Commented Jul 17 at 15:33
  • 2
    Note that if performance is important then a simple loop-based function can be faster when your array of values is not on a worksheet: stackoverflow.com/a/18769246/478884 Commented Jul 17 at 19:10

3 Answers 3

2

Application.Match

  • The following two usually work the same:

    If IsNumeric(Application.Match("foo", MyArray, 0)) Then
    If Not IsError(Application.Match("foo", MyArray, 0)) Then
    
  • If you pass correct parameters, the result can only be an integer (whole number) or a #N/A error.

  • You shouldn't be paranoid about a different error than the #N/A error (Error 2042 in VBA).

  • If you can't help it, here's something to play with.

Sub Test()
    
    ' Uncomment or out-comment any of the following lines before 'Get the index.'
    ' and observe the behavior.
    
    On Error GoTo ClearError
    
    Dim MyArray As Variant
    'Dim MyArray() As Variant
    'Dim MyArray() As String
    'Dim MyArray As String
    
    MyArray = Array("fee", "fi", "fo", "fum")
    
    ' Get the index.
    Dim Index As Variant: Index = Application.Match("fee", MyArray, 0)
    
    ' Display the result.
    If IsNumeric(Index) Then
        Debug.Print "Pass: Index = " & Index
    Else
        If Index = CVErr(xlErrNA) Then ' xlErrNA = 2042
            Debug.Print "No match"
        Else
            Debug.Print "Something unexpected happened (" & CStr(Index) & ")!"
        End If
    End If

ProcExit:
    Exit Sub
ClearError:
    MsgBox "Run-time error " & "[" & Err.Number & "]:" & vbLf & vbLf _
        & Err.Description, vbCritical
    Resume ProcExit
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

Use XMATCH for the unsorted array.

Use CVErr function with 'xlErr*' constants to detect errors.

Dim MyArray

MyArray = Array("fo", "fee", "fum", "fi")

If Application.XMatch("fee", MyArray) <> CVErr(xlErrNA) Then
   Debug.Print "Pass"
End If

Comments

0

Use IsError, because Application.Match can return an error (#N/A), which If can't handle directly:

Dim result As Variant
Dim MyArray

MyArray = Array("fee", "fi", "fo", "fum")
result = Application.Match("foo", MyArray, 0)

If Not IsError(result) Then
    Debug.Print "Pass"
Else
    Debug.Print "Not Found"
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.