Application.WorksheetFunction.Match method

calendar_today Asked Nov 26, 2013
thumb_up 9 upvotes
history Updated April 14, 2026

Direct Answer

You are getting this error because the value cannot be found in the range. String or integer doesn't matter. Best thing to do in my experience is to do a check first to see if the…. This is a 19-line Excel VBA snippet, ranked #247th of 303 by community upvote score, from 2013.


The Problem (Q-score 3, ranked #247th of 303 in the Excel VBA archive)

The scenario as originally posted in 2013

I have seen a lot of Topics to the “unable to get the match property of the worksheetfunction class” problem. But I can’t get my code fixed.

Why isn’t this code work?

rowNum = Application.WorksheetFunction.Match(aNumber, Sheet5.Range("B16:B615"), 0)

But a few rows higher this code works:

rowNum2 = Application.WorksheetFunction.Match(originCode, Sheet7.Range("B10:B17"), 0)

The only difference between my two lines is that in rowNum2 I used a String for look up and in rowNum a integer.
Is it possible that the look up Value needs to be a String?

@Update on my Problem

Select Case service
Case "Low Cost"
MsgBox Sheet5.Cells(16, "B") 'Gets value 0.5
Set Rng = Sheet5.Range("B16:B615")

   If Not IsError(Application.Match("0.5", Rng, 0)) Then 'But jumps to Else
    rowNum = Application.Match(Weight, Rng, 0) 'Weight = 0.5
    MsgBox rowNum
Else
    MsgBox "error"
End If



Case "Standard"

Case "Express"

Case Else

End Select

@UPDATE 2

!!! Take care that “0.5” is a String and not 0.5
So 0.5 is not “0.5” (that was my error in the code)

Why this Range / Worksheet targeting trips people up

The question centers on reaching a specific cell, range, or workbook object. In Excel VBA, this is the #1 source of failures after activation events: every property (.Value, .Formula, .Address) behaves differently depending on whether the parent Workbook is explicit or implicit.


The Verified Solution — niche answer (below median) (+9)

19-line Excel VBA pattern (copy-ready)

You are getting this error because the value cannot be found in the range. String or integer doesn’t matter. Best thing to do in my experience is to do a check first to see if the value exists.

I used CountIf below, but there is lots of different ways to check existence of a value in a range.

Public Sub test()

Dim rng As Range
Dim aNumber As Long

aNumber = 666

Set rng = Sheet5.Range("B16:B615")

    If Application.WorksheetFunction.CountIf(rng, aNumber) > 0 Then

        rowNum = Application.WorksheetFunction.Match(aNumber, rng, 0)

    Else
        MsgBox aNumber & " does not exist in range " & rng.Address
    End If

End Sub

ALTERNATIVE WAY

Public Sub test()
    Dim rng As Range
    Dim aNumber As Variant
    Dim rowNum As Long

    aNumber = "2gg"

    Set rng = Sheet5.Range("B1:B20")

    If Not IsError(Application.Match(aNumber, rng, 0)) Then
        rowNum = Application.Match(aNumber, rng, 0)
        MsgBox rowNum
    Else
        MsgBox "error"
    End If
End Sub

OR

Public Sub test()
    Dim rng As Range
    Dim aNumber As Variant
    Dim rowNum As Variant

    aNumber = "2gg"

    Set rng = Sheet5.Range("B1:B20")

    rowNum = Application.Match(aNumber, rng, 0)

    If Not IsError(rowNum) Then
        MsgBox rowNum
    Else
        MsgBox "error"
    End If
End Sub

Error-handling details to lift with the snippet

This answer wires error flow through MsgBox / Err.Description. Keep that intact: stripping it to “make it cleaner” removes the signal you’ll need when the macro fails silently on a user machine.


When to Use It — classic (2013–2016)

Ranked #247th in its category — specialized fit

This pattern sits in the 97% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Excel VBA archive for a higher-consensus alternative.

What changed between 2013 and 2026

The answer is 13 years old. The Excel VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

This is a below-median answer — when does it still fit?
expand_more

Answer score +9 vs the Excel VBA archive median ~4; this entry is niche. The score plus 3 supporting upvotes on the question itself (+3) means the asker and 8 subsequent voters all validated the approach.

Does the 19-line snippet run as-is in Office 2026?
expand_more

Yes. The 19-line pattern compiles on Office 365, Office 2024, and Office LTSC 2026. Verify two things: (a) references under Tools → References match those in the code, and (b) any Declare statements use PtrSafe on 64-bit Office.

Published around 2013 — what’s changed since?
expand_more

Published 2013, which is 13 year(s) before today’s Office 2026 build. The Excel VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Excel VBA pattern ranks just above this one at #246?
expand_more

The pattern one rank above is “2 ways for "ClearContents" on VBA Excel, but 1 work fine. Why?”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 3, Answer-score 9, original post 2013, ranked #247th of 303 in the Excel VBA archive. Last regenerated April 14, 2026.