Problem: I have to lookup two values. I need to match both a company code and a cost center.
- Match both the Company and Center.
Strategy: There are three solutions to this problem: (a) Concatenated key, (b) OFFSET, or (c) SUMIFS. The concatenated key will only work if you are allowed to add a new column to the left of column C. The SUMIFS will only work if the value to be returned is numeric. The OFFSET will only work if all of the company codes are sorted together as shown above.
With a concatenated key, you will insert a new column before the Amounts in column C. You want to join column A, a unique separator, and column B. For example, =A2&"-"&B2 would produce a key of 100-1010.
- Build a concatenated key in your data.
The separator text is optional. In real life, you might have two company/center combinations that would look the same once joined. Using a dash in between will prevent this ambiguity.
- Using a dash prevents the identical key fields in red.
Once you have the concatenated key in the lookup table, you can join the key fields on-the-fly in your VLOOKUP formula:
- Join the two key fields as the first argument of VLOOKUP.
As mentioned earlier in this topic, this method only works if you are able to add the concatenated key to your data. It is fine to hide column C so no one see it, but you have to have the field there.
Alternate Strategy: If the value that you are trying to return is numeric, you can use DSUM or SUMIFS. For details, see "Calculate Based on Multiple Conditions" on page 192.
- The new SUMIFS would solve the problem.
Alternate Strategy: Use the OFFSET function. Purists will argue that OFFSET is a volatile function and therefore slows down your calculation times. However, OFFSET will often solve problems where you need to reference a range that is moving or resizing.
OFFSET is used to point to a range. The location and size of the range is calculated as the formula is being calculated.
OFFSET allows five arguments. At least one of the four final arguments should be a formula that is calculated on the fly. When OFFSET is set up to return a range of cells, you will find yourself using OFFSET inside of another function such as SUM, or in this case, inside of VLOOKUP.
The syntax is =OFFSET(Reference,Rows Down from There, Columns Right from There, Rows Tall, Columns Tall). For example, you could start with a reference of B1, move down N rows, move right 0 rows, make the range be 7 rows tall and 2 columns wide.
In the next figure, a MATCH function in F5 figures out where the lookup table for this company begins. The COUNTIF in F6 figures out how tall the lookup table should be. Both of these numbers will feed into an OFFSET function that is shown for illustration in F7. The actual formula is found in F9, where the OFFSET is used to describe the lookup table in the VLOOKUP formula.
- OFFSET is slow, but versatile.