Welcome to Excel Magic Trick number 1,231. If you want to download this workbook, 1,230 and 1,231, start or the finished file, click on the link below the video. Wow, we have a wild one here. We actually have a lookup situation. There are 36 tables here, and each table represents a particular region and a particular product group. If you look at this first one here, it says West freestyle, then West Belling group. Those are product groups. If you scroll down, you see West MTA, that's the last product group in this region, and it starts off with the next region, East freestyle, East Bellon group. So there are 36 tables. That is simply amazing. How in the world are we going to be able to get the discount rate from each one of these tables?
Now let's go over to the start file. I'm going to control tab, and the trick is that each one of these is a transaction, and somehow the information for which table to go to has to come from our transaction, and it does here. There's the region and the product group. In this particular situation, we have some table names up here, right, and notice it's not exactly the same as West and Doublers. But inside our formula, we can do a number of things. If we were going to search through this column, or even search through the list of different tables, we can take West and join it to an underscore and get Doublers to match, either from this list or over here. We also, since it's Table one, Table two, we can't type in anything, so we're going to use a wild card. Now we're going to see three different methods, and we're going to start with the question asked about the CHOOSE function.
Well, let's think about this. The CHOOSE function is great because if we can give it an index number, the number one all the way to 36, we can just list all 36 tables right in our formula, where it says value one, value two, value through value four. So the trick for CHOOSE is we need to get 1 to 36. Now how are we going to do that when we're given West and freestyle, West and Doublers? The way I'm going to do it is I'm going to actually use the lookup function MATCH. MATCH is going to need a lookup value, and we're going to have to get it from West and freestyle, and I'm going to look through this list of tables. So I'm going to go ahead and start off. I'm going to say, hey, region, that's a relative cell reference four to my left, and that's the join symbol in double quotes, underscore, and double quotes, and then join to freestyle. Right now I cannot take West_freestyle and match it directly, but since there's always going to be some stuff before, I'm going to use a wild card in double quotes, asterisk, which means zero or more characters, and double quote, and then I'm going to join it. That is pretty wild. From these two values, I'm going to be able to look that up, comma, within this entire list of tables, control shift down arrow, F4, comma, and the match type is going to be zero for exact match. Right there, close parentheses, control enter, and double click and send it down. So from this table over here, I'm getting the position. It found West freestyle as the first position in this list over here. West Doublers, it got an E, that means it was in the eighth position. Now MATCH thinks of it as a position, but now we can put that inside of CHOOSE, and CHOOSE is another lookup function. Again, it's just going to take that number, and the index number runs from 1 to 36, and now I have to list the tables 36 times.
I'm going to go ahead and do the first couple ones here. I'm going to highlight that and F4 to lock it. Notice it says value one, comma, value two. I'm going to get the second table, F4 to lock it, comma, the third table, highlight, F4 to lock it, comma, and I'm off to the fourth value. All right, now I've listed all 36 there. The disadvantage here is that's a lot of clicking, and perhaps I made an error because there's so much manual work. The upside is you don't have to know how to use any special OFFSET or INDIRECT function, which are two other solutions we'll see. You just get your 1 to 36 index number, and then you list all of your tables.
Now all CHOOSE is going to do is deliver one of these tables. If I enter this, control enter, double click and send it down, it's not going to show me anything, because it's delivering the whole table. But let's check this out. Let's highlight this entire CHOOSE, F9, and sure enough, you see the first one: Quad 8.2, Quad 8.2, Carota 13, Carota 13. So it's delivering the right table. If I come down here to the West Belling group, that's this one right here. If I highlight this entire thing and press F2, you can see we get Bellon 99.9% and Bellon Dancer 14.7%. So the CHOOSE function is doing its job; it's delivering the table. Now we can simply put it inside of VLOOKUP. Remember, I need to now look up the product, comma, and that entire thing is just delivering a table. One, two, it's always going to be in the second column, the thing I'm getting and bringing back to the cell. So I type comma two, that's the column with the discount, comma, and then I'm going to use zero for exact match, close parentheses, control enter, double click and send it down. Wow, that's a pretty wild formula, looking up from 36 different tables.
Now we can take a slightly different approach. One way we can think about this is notice we could actually look through this entire range with our approximate match, with our asterisk and our underscore, and find the position of either West freestyle or West Bellon group. We could use that position in the OFFSET function to then go and get the right table for each product. So let's look at the OFFSET function. The reference is the starting position. We're always going to start right in A7, so I'm going to lock it with F4. Then I need to know how many rows from that starting position to go down, and this is where we use MATCH: double quotes, asterisk, double quotes, join it to our region, and double quotes, underscore, double quotes, and join it to our product group. There's our lookup value. Comma, the lookup array: highlight, and watch this. I'm scrolling down, and I click on the very last possible range right here, but I hold Shift before I click there, so now I've highlighted it all the way down. F4 to lock it. Now I have that whole range, comma, zero for exact match, close parenthesis.
Right now we have our starting position and the number of rows to go down. Think about this: West freestyle, since MATCH is going to find it as the first position, that cell right there is the starting position, but it will force us to go down one. That'll be the starting position, comma. Then columns: that's how many columns to the right or left from the starting position. We don't want to go any, we're in the correct column, so I leave it empty. The height is always going to be four, and in fact this is set up so some are three and some are four, so four will work. Comma, the width is always going to be two, close parentheses. Now let's enter this, control enter, and we get exactly the same value, because it's doing something similar to CHOOSE, but now it's using OFFSET to define a variable range. If I hit F9, of course it finds Quad 8.2 and Carlota 3.7, and those two zeros at the end will not hurt us in this case. Later, when there's a table that has four products, it will show the product names. Escape, and if I come down to the West Bellon group, highlight, F9, you can see we get Bellon 9.9 and Bell and Dancer 14.7 right there. If I look at the West Sunshine group, I can see down here that's four. So for West Sunshine group, if I highlight this OFFSET and press F9, you can see the last two items, Sunspot and 5.9, are not zero. It picks up that fourth item. Now we can simply use OFFSET inside a VLOOKUP. I select the product, comma, there's the whole table delivered by OFFSET, comma 2, comma 0, close parentheses, control enter, double click and send it down.
Now, anytime I do something like this, I always like to check. Watch this. I'm going to say equals, hey, are you equal to this one? Control enter, double click and send it down. I don't want to check this manually, so I'm going to simply do a quick COUNTIFS up here and highlight the entire range, control shift down arrow, control backspace, comma, and then FALSE, which is neither text nor number, it's its own value. I simply put FALSE in as the criteria to see if there are any FALSEs, meaning the two formulas were different. It looks like I didn't get any. All right, that was a good check. Control shift down arrow, delete.
There's another way we can do this too. Instead of highlighting each range like I did in the CHOOSE section, if I used the Define Name feature, I could name this range West_freestyle and this range West_Belling_group. Then I could use the INDIRECT function to take the region underscore and freestyle to look up, in essence, that defined name. Watch this. I'm going to cheat. I highlight this, control C, Escape, then I click up in the Name Box, control V, and Enter. Now if I highlight that, you can see sure enough it's West_freestyle. I have to do that for each one of these: control C, Escape, click up here, control V, Enter. I'll do it for the first three here. I highlight this, come up to the Name Box, control V, and Enter. I've already done the other 33, so here they all are: East_Doublers, and you can see there's the East_Doublers, the East_MTA, the East_freestyle, that's a three-row group.
All right, check this out. I almost think I like this solution best. I'm going to create that defined name, in essence, from the transactional information. I've got the region, ampersand, double quote, underscore, double quote, and parentheses, and I join it. When I enter this, double click and send it down, that is actually text. So if I put this into a formula expecting the formula to see it as a reference, it won't work. But there's a function called INDIRECT, and all INDIRECT does is take some text that represents a reference, remember that's a defined name, and converts it from text into the reference, or in our case, the defined name. Control enter, double click and send it down. We get the same exact values, but I can highlight this, F9, and sure enough there's the first table. Escape. If I come down here, this is the West Bellon group, so I highlight this, F9, and sure enough it got the right table. Again, we're using the INDIRECT function on some text, and we've already defined the defined names.
Now I can simply put this inside of VLOOKUP. I'm looking up the product, comma, that whole table from INDIRECT, which works inside our VLOOKUP, comma 2, comma 0, close parentheses, control enter, double click and send it down. Equals, are you equal to that? Double click, send it down. I check in here: equals COUNTIFS, arrow, arrow, control shift down arrow, control backspace, comma, FALSE, control enter. I got a zero, so it looks like the formulas match.
So we can use CHOOSE with MATCH, using a wild card and an underscore, and a bunch of tables inside our VLOOKUP, in essence looking up one of the 36 tables over here. We used OFFSET to define a different table each time: we had a starting position, went down a certain number of rows, and had a table to find as four rows by two columns. Finally, we used defined names and the INDIRECT function to get from our transactional information, West freestyle, to deliver the defined name to VLOOKUP. Wow, that's a lot of fun looking up from 36 different tables. We'll see you next trick.
