I need a function in VBA that finds the row number based on 2 where clauses.
Here is the Excel sample:
** A B C D**
1 id1 day1 val1 xxx
2 id2 day1 val2 xxx
3 id3 day1 val3 xxx
4 id1 day2 val1 xxx
5 id2 day2 val2 xxx
6 id3 day2 val3 xxx
I need to find the row number (in this case row number is 2) where B = "day1" and A = "id2".
Based on the row number, I need to further get the values of other columns, i.e. C2, D2
Hope that the question is clear.
Thank you!
With your data setup like that, you can use the MATCH function to get the row number:
If there are no matches for those criteria, the formula will return an #N/A error. You can also change the criteria to be cell references, for example:
For the second part of your question, returning values with the found row number, you can use the INDEX function to return a value from a column. So pretending the Match formula is in cell H1, these two formulas will return the value from column C and D respectively:
Alternately, you could put it all into a single formula:
And if you don't want to be looking at errors, you can use an IFERROR on excel 2007+
Error checking for Excel 2003 and below:
[EDIT]: I am including a VBA solution per user request. This uses a find loop, which is very efficient and flexible, and shows how to extract values from other columns once a match has been found:
In VBA you can do a brute force check like the following: