Reputation: 45
I have a following code and i have "name" in cells B11 , B19 and B25 and i was expecting a result of 19 or 25 with the code but it returns Y=1. Could anyone tell me what is the problem?.
Sub mat()
Y = Application.Match("name", ActiveSheet.Range("B19:B30"), 0)
MsgBox Y
End Sub
Upvotes: 0
Views: 22852
Reputation: 1
add slgn
Sub mat()
Y = Application.Match(slng(range("a4").value), ActiveSheet.Range("B19:B30"), 0)
MsgBox Y
End Sub
ex2:
Application.WorksheetFunction.Match(CLng(TextBox1.Text), sheet110.Range("B6:B" & ls), 0)
Upvotes: 0
Reputation: 3068
Application.Match("name", ActiveSheet.Range("B19:B30"), 0)
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
So parsing the parameter ActiveSheet.Range("B19:B30")
means that B19
equals relative position =1
.
Upvotes: 1
Reputation: 44871
According to the documentation for MATCH:
MATCH returns the position of the matched value within lookup_array, not the value itself.
and with 0
as the optional third argument (match_type
):
If match_type is 0, MATCH finds the first value that is exactly equal to lookup_value. Lookup_array can be in any order.
So the returned 1
refers to the position on B19
in the array Range("B19:B30")
and the code sample is indeed behaving as expected.
Upvotes: 4