I'm trying to use Application.match to find the column in which a value in range A1:Z1 and A2:Z2 are matched. For example the 1st row contains different fruit names, and the 2nd row contains colours. So, say I am looking for a column with a blue banana, something like this should work:
mycolumn = Application.Match("Banana" & "Blue", Worksheet("Coloured_Fruit").Range(A1:Z1) & Worksheet("Coloured Fruit").Range(A2:Z2), 0)
In order to match blue banana in the following data sheet:
A1:banana B1:apple C1:banana D1:orange
A2:green B2:blue C2:blue D2:green
This should return C, because the C column is the one representing a blue banana.
But I get a type mismatch. The ranges are on a different worksheet to the code that's doing the match. This works fine when I am only trying to match one row, but not for two. A web search suggests the above line should work.
I've tried using Evaluate on the expression, but that doesn't work either.
Any suggestions how to do this?
The MATCH in your code fails because the "&" operator only works on strings in VBA. In Excel worksheets, an "&" can join ranges if it is entered as part of an array formula.
Modifications to the MATCH formula that seem like they should work in VBA also return a "Type Mismatch" error. These include putting the two ranges together by assigning them to a single Range variable (Range("A1:Z1","A2:Z2")), or using the UNION function ( Union("A1:Z1","A2:Z2") ) for the same purpose. Qualifying either of those with ".Value" does not help, either.
The following code does the trick:
Focusing on the MATCH expression (and stripping out the extra double-quotes needed for the EVALUATE function),
Each resolves to an array of booleans {FALSE, FALSE, ..., TRUE, etc.}, with a TRUE in the relative position in the range of each cell that contains the target string.
Note that using this approach the comparisons need not be limited to just two.
The final 0 in the MATCH formula specifies that the match be an exact one.
Since the subroutine returns a single value, you may want to consider recasting it as a VBA function.