I don't think there is a single formula that will do what you want but a pair of macro functions can do the job easily.
The first macro function (e.g., Quantity_Cost) is a subroutine that accepts a food name as a string and a quantity as an integer. It looks up the item price in for that name in Menu Costs, multiplies that value by the quantity, and returns the product to the calling macro.
The second macro function (e.g., Total_Cost) is the main routine. It extracts the text in the corresponding cell in the Food Option column. It determines if this text represents a single Food (as in rows 1, 2, and 3 of your sample) or it represents several Foods, each enclosed in quotes, and the entire list enclosed in brackets (as in row 5)
- For a single Food, call Quantity_Cost passing the text and the quantity (not shown in your sample). Return the value from this call to the cell from which Total_Cost was called (the Cost column in your sample).
- For multiple foods, you need a loop.
- Find the first quote and note its location. If none, exit the loop.
- Find the next quote and note its location. If none, the data is malformed.
- Extract the text between, but not including, the quotes.
- Call Quantity_Cost passing the extracted text and the quantity.
- Add the value returned from this call to an accumulator.
- Repeat the loop
- Add the value returned from this call to an accumulator.
- Call Quantity_Cost passing the extracted text and the quantity.
- Extract the text between, but not including, the quotes.
- Find the next quote and note its location. If none, the data is malformed.
- Find the first quote and note its location. If none, exit the loop.
- Return the value in the accumulator to the cell from which Total_Cost was called.
You can add additional error checking as you wish. For example: check for both brackets; check for a comma between quoted foods; verify the Food exists in Menu Costs; etc.
Answer from Barry Schwarz on learn.microsoft.com=SUMPRODUCT(VLOOKUP(T(IF({1},$B$2:$B$6)),$F$2:$G$9,2,)*$C$2:$C$6*($A$2:$A$6=J2))
Somebody gives the above solution, it's great.
The answer seems to tell that(I just presume, not sure):
IF({1},$B$2:$B$6)convert cells to static array.the first argument
lookup_valueinVLOOKUPcan be array.
If you add a helper column in column D to get the price total for each row you can do it with sumif:
Put this in D2 and drag down:
=INDEX(G:G,MATCH(B2,F:F,0),1)*C2
Then this in K2:
=SUMIF(A:A,J2,D:D)
You can drag this down as well if you want to add another name to total in the next line.
Wanted to create an automated dashboard where the total price is calculated for every item as and when it is entered. Can someone help with the formula?
We sell a product that has a unit price that decreases as the volume purchased increases and want to add an "unlimited" price that's based on a specific volume.
Currently, the volume increments are 0-40k, 40k-70k, 70k-100k, 100k-1M, and above 1M in column A and each increment has a unit price in column B.
I want to change that last increment to 1M-6M and add a row for 6M+ but use the specific word "unlimited" in column A.
Today, the formula to calculate the cost is: =IF(B14>A6, A3*B3+(A4-A3)*B4+(A5-A4)*B5+(A6-A5)*B6+(B14-A7)*B7, IF(B14>A5, A3*B3+(A4-A3)*B4+(A5-A4)*B5+(B14-A5)*B6, IF(B14>A4, A3*B3+(A4-A3)*B4+(B14-A4)*B5, IF(B14>A3, A3*B3+(B14-A3)*B4, B14*B3))))/B14/5000
First of all, shouldn't we be using a VLOOKUP instead of that ridiculously long formula?
... and, is there a way to create a formula that looks for the word "unlimited" instead of a number to calculate a price?