Lookup Table with Wild Cards

AlexGuest

New Member
Joined
Aug 19, 2011
Messages
12
Hello. I am trying to create a formula that will lookup a value from a table based on 2 criteria, but in certain cases I only want 1 criteria. The table would like this...

In cells A1:A5
Blue
Red
Green
*
Black

In cells B1:B5
Dog
Fish
Fish
Fish
Dog

In cells C1:C5
Result1
Result2
Result3
Result4
Result5

If the lookup values were Red and Fish, I want the formula result to be Result2. Green Fish = Result3. Any other color Fish should be Result4.

I couldn't use SUMPRODUCT since I'm looking up text and an Index Match array doesn't recognize the *. So I came up with the following...

=LOOKUP(10^10,SEARCH(A1:A5,A7)*SEARCH(B1:B5,B7),C1:C5)
Cell A7 is the lookup value for column A
Cell B7 is the lookup value for column B

This almost works perfectly except that Red Fish returns Result4 instead of Result2 because the lookup function returns the last match. So, my questions...
1) Is there a way to modify my formula so that it returns the first match?
2) Is there a another formula that would work or a way to do this without the * wildcard?

If not, I think I'll have to invert my table which i'm hoping to avoid.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Row\Col
A​
B​
C​
D​
E​
F​
G​
1​
Blue Dog Result1 red fishResult2
2​
Red Fish Result2 green fishResult3
3​
Green Fish Result3 * fishResult4
4​
* Fish Result4
5​
Black Dog Result5

In G1 control+shift+enter, not just enter, and copy down:
Rich (BB code):
=INDEX($C$1:$C$5,MATCH("~"&E1,IF($B$1:$B$5=F1,$A$1:$A$5),0))

Note. If you ever need a large constant as a look up value in the last value formulas, use a constant of Excel itself: 9.99999999999999E+307.
 
Upvote 0
Thank you for the quick response Aladin. Unfortunately, the index-match-array approach does not seem to work with an * in the table. I apologize if my post wasn't clear. You have an * as a lookup value (cell E3 in your example). What I am looking for is a formula that will return Result4 if Orange is in E3 in your example. Basically, a red fish is Result2, a green fish is Result3 and any other fish is Result4.

If my formula can be modified to return the first match instead of the last match, and there is another way that doesn't use the * in the table, please let me know.
 
Upvote 0
Thank you for the quick response Aladin. Unfortunately, the index-match-array approach does not seem to work with an * in the table. I apologize if my post wasn't clear. You have an * as a lookup value (cell E3 in your example). What I am looking for is a formula that will return Result4 if Orange is in E3 in your example. Basically, a red fish is Result2, a green fish is Result3 and any other fish is Result4.

If my formula can be modified to return the first match instead of the last match, and there is another way that doesn't use the * in the table, please let me know.

Row\Col
A​
B​
C​
D​
E​
F​
G​
1​
Blue Dog Result1 red fishResult2
2​
Red Fish Result2 green fishResult3
3​
Green Fish Result3 orange fishResult4
4​
* Fish Result4
5​
Black Dog Result5

In G1 control+shift+enter, not just enter, and copy down:
Rich (BB code):
=INDEX($C$1:$C$5,MATCH(IF(ISNA(MATCH(E1,$A$1:$A$5,0)),"~*",E1),
    IF($B$1:$B$5=F1,$A$1:$A$5),0))
 
Upvote 0
That works perfectly, but could you please help me make the formula more dynamic so I can...
- Have an * in column B (in addition to an * in column A of another row). In your example, if B5 is changed to an *, I'd like Black Goat to return Result5.
- Expand from this 2 criteria column example to more criteria columns (each column allowing *'s).

I was hoping to be able to figure out how to expand your formula on my own, but wasn't able to figure it out. If you could provide an explanation on how your formula works, that would be helpful too.

Thanks again Aladin
 
Upvote 0
That works perfectly, but could you please help me make the formula more dynamic so I can...
- Have an * in column B (in addition to an * in column A of another row). In your example, if B5 is changed to an *, I'd like Black Goat to return Result5.
- Expand from this 2 criteria column example to more criteria columns (each column allowing *'s).

I was hoping to be able to figure out how to expand your formula on my own, but wasn't able to figure it out. If you could provide an explanation on how your formula works, that would be helpful too.

Thanks again Aladin

In G1 control+shift+enter and copy down:
Rich (BB code):
=INDEX($C$1:$C$5,MATCH(IF(ISNA(MATCH(E1,$A$1:$A$5,0)),"~*",E1),
    IF($B$1:$B$5=IF(ISNA(MATCH(F1,$B$1:$B$5,0)),"*",F1),$A$1:$A$5),0))

Try to apply F9 to the selected bits of the formula on the formula bar in order to see to what those bits evaluate to. This might help you to arrive at an understanding how it works.
 
Upvote 0
That works for 2 criteria columns. I've been staring at this for an hour trying to figure out how to expand it to a 4 criteria column example. I haven't been able to figure it out. How can the formula be changed so that a 4 criteria table can be used. See example...

Row\Col A B C D E F G H IJ K
1BlueDogAppleChicagoResult1RedfishBaconNYResult2
2RedFish*NYResult2GreenfishSausageChicagoResult4
3GreenFishPear*Result3BlackDogBaconChicagoResult5
4*Fish**Result4GreenFishPearParisResult3
5Black**ChicagoResult5BlackCatMeatChicagoResult5

<tbody>
</tbody>

Similar to the previous examples, columns A to D are the criteria columns, column E is the desired result. Columns G to J are lookup values and column K is where I need a formula that returns the applicable result from column D?

I'm open to using a different approach if anyone has one but I need to have a table of rules similar to this that I will update frequently.
 
Upvote 0
We are raising the stakes, aren't we?

Row\Col
A​
B​
C​
D​
F​
G​
H​
I​
J​
K​
M​
1​
Blue Dog Apple Chicago Result1 Red fish Bacon NY Result2
2​
Red Fish * NY Result2 Green fish Sausage Chicago #N/A
3​
Green Fish Pear * Result3 Black Dog Bacon Chicago #N/A
4​
* Fish * * Result4 Green Fish Pear Paris Result3
5​
Black * * Chicago Result5 Black Cat Meat Chicago Result5

In M2 control+shift+enter, not just enter, and copy down:
Rich (BB code):
=INDEX($F$1:$F$5,MATCH(1,IF($A$1:$A$5=IF(ISNA(MATCH(H1,$A$1:$A$5,0)),"*",H1),
    IF($B$1:$B$5=IF(ISNA(MATCH(I1,$B$1:$B$5,0)),"*",I1),
    IF($C$1:$C$5=IF(ISNA(MATCH(J1,$C$1:$C$5,0)),"*",J1),
    IF($D$1:$D$5=IF(ISNA(MATCH(K1,$D$1:$D$5,0)),"*",K1),1)))),0))

If these results are not admissible, consider the following set up...

Row\Col
A​
B​
C​
D​
E​
F​
H​
I​
J​
K​
L​
1​
* Fish * * *|Fish|*|* Result4 Green Fish Pear Paris Green|Fish|Pear|Paris
2​
Black * * Chicago Black|*|*|Chicago Result5 Black Cat Meat Chicago Black|Cat|Meat|Chicago
3​
Blue Dog Apple Chicago Blue|Dog|Apple|Chicago Result1 Red fish Bacon NY Red|fish|Bacon|NY
4​
Green Fish Pear * Green|Fish|Pear|* Result3 Black Dog Bacon Chicago Black|Dog|Bacon|Chicago
5​
Red Fish * NY Red|Fish|*|NY Result2 Green fish Sausage Chicago Green|fish|Sausage|Chicago

In E1 just enter and copy down:
Rich (BB code):
=A1&"|"&B1&"|"&C1&"|"&D1

In L1 just enter and copy down:
Rich (BB code):
=H1&"|"&I1&"|"&J1&"|"&K1

Now sort A:L on E in ascending order...

In M1 just enter and copy down:
Rich (BB code):
=LOOKUP(9.99999999999999E+307,SEARCH($E$1:$E$5,L1),$F$1:$F$5)

If done properly, the results in M would look like below:

Row\Col
M​
1​
Result3
2​
Result5
3​
Result2
4​
Result5
5​
Result4
 
Upvote 0
The first option doesn't help due to the #N/A's. A couple questions on the second option...
1) Why is sorting necessary? Doesn't the lookup formula find the first match starting from the last row of the table searching backwards/up?
2) Is there a way to modify the formula so that it finds the first match starting from the first row of the table searching down?
 
Upvote 0
The first option doesn't help due to the #N/A's.


Delivering a blank instead #N/A there is no problem. The question is whether such is admissible.

A couple questions on the second option...
1) Why is sorting necessary? Doesn't the lookup formula find the first match starting from the last row of the table searching backwards/up?

We don't want the set up stop too early while trying to match.

2) Is there a way to modify the formula so that it finds the first match starting from the first row of the table searching down?

It's a last match construct.
 
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,280
Members
449,220
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top