Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.WorksheetFunction.Match method

Tags:

excel

vba

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)

like image 970
user3035828 Avatar asked Nov 26 '13 10:11

user3035828


People also ask

What is application WorksheetFunction match?

Match returns the position of the matched value within lookup_array, not the value itself. For example, MATCH("b",{"a","b","c"},0) returns 2, the relative position of "b" within the array {"a","b","c"} . Match does not distinguish between uppercase and lowercase letters when matching text values.

What does application match do in VBA?

The MATCH function looks for the position of the lookup value in the selected array table. The MATCH function is mainly used with the VLOOKUP function to supply the column index number using the column heading automatically. The MATCH function is available as a worksheet function in VBA.

Is there a match function in VBA?

VBA Match Function looks for the position or row number of the lookup value in the table array i.e. in the main excel table. For example, VLOOKUP, HLOOKUP, MATCH, INDEX, etc. These are the lookup functions that are more important than others.

How do you use match formula?

The MATCH function searches for a specified item in a range of cells, and then returns the relative position of that item in the range. For example, if the range A1:A3 contains the values 5, 25, and 38, then the formula =MATCH(25,A1:A3,0) returns the number 2, because 25 is the second item in the range.


1 Answers

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
like image 162
Sam Avatar answered Sep 27 '22 20:09

Sam