Lece
New Member
- Joined
- Jan 14, 2012
- Messages
- 15
Hi everyone!
I am trying to code the following Microsoft's example in VBA: How to use the INDEX and MATCH worksheet functions with multiple criteria in Excel. On my worksheet (i.e. in Excel GUI) everything works fine. But I can't make it work in VBA.
For instance, I have a table with 3 columns: Name, Last Name and Profession.
I need to pick a record with Adam (A2) Smith (B2) who is an economist (C2).
Here's my worksheet formula. It's an array formula so after typing I Ctrl+Shift+Enter it.
In VBA, first of all, I define result as Variant (because I will keep getting the "Type mismatch" error since the Match function can return not only an integer, but also an error if nothing found -- this info might be useful in case someone will be googling the same problem):
I've tried the following:
The code above returns "Type mismatch" error since, I guess, the "&" (ampersand) concatenates only string values which nameR, lastnameR etc. are not.
Still no success: it fills the target cell with a #VALUE! error.
Please help me. Thank you in advance.
I am trying to code the following Microsoft's example in VBA: How to use the INDEX and MATCH worksheet functions with multiple criteria in Excel. On my worksheet (i.e. in Excel GUI) everything works fine. But I can't make it work in VBA.
For instance, I have a table with 3 columns: Name, Last Name and Profession.
I need to pick a record with Adam (A2) Smith (B2) who is an economist (C2).
Here's my worksheet formula. It's an array formula so after typing I Ctrl+Shift+Enter it.
Code:
{=MATCH(A2&B2&C2,A5:A9&B5:B9&C5:C9,0)}
Code:
Dim result As Variant
Code:
name = Range("A2").Value
nameR = Range("A5:A9")
lastname = Range("B2").Value
lastnameR = Range("B5:B9")
profession = Range("C2").Value
professionR = Range("C5:C9")
result = Application.WorksheetFunction.Match(name & "&" & lastname & "&" & profession, nameR & "&" & lastnameR & "&" & professionR , 0)
Code:
match_formula = "Match(" & name & "&" & lastname & "&" & profession & ", " & nameR.Address & "&" & lastnameR.Address & "&" & professionR.Address & ", 0)"
result = Evaluate(match_formula)
Please help me. Thank you in advance.
Last edited by a moderator: