1

I am trying to calculate an amount based on a formula that has a different number of arguments for each calculation. And, each formula is expressed as a string that is based on the column names at the top of the sheet (e.g. item1). Each argument feeds a XLOOKUP to get the value and then that value gets added to the one before it (a+b+c+d).

            |item1|item2|item3|item4|item5|
itemAmount    100    50    25     2    3
rate           ?     ?      ?     ?    ?

formula for item1 rate = item1
formula for item2 rate = item1, item2
formula for item3 rate = item3
formula for item4 rate = item3, item4

formulas are expressed in a comma-separated array in a cell in the worksheet.

Desired Results:
rate for item1 = 100
rate for item2 = 150
rate for item3 = 25
rate for item4 = 27
rate for item5 = 180

The below works until I have 4 or more arguments to add together. e.g. item1, item2, item3, item4 will not get calculated.

Is there a way to implement this that truly loops through the formula arrays? I tried REDUCE() but that didn't seem to recognize text.

What I have so far is a combination of LAMBDA() and LET(). I used LAMBDA() to create a reusable formula for the users that will get inserted in dozens of places in the workbook.

=LAMBDA(
TaskFormula,TaskNamesRange,RateName,RateNamesRange,ReturnRange, 

LET( 
tfl, 
TaskFormula, 

tnrng, 
TaskNamesRange, 

rateType,
TRIM(RIGHT(RateName,LEN(RateName)-FIND(">> ",RateName)-1)), 

rateNameRng, 
RateNamesRange, 

retRng, 
ReturnRange, 

a, 
XLOOKUP(IFERROR(TRIM(LEFT(tfl,FIND(", ",tfl)-1)),tfl),tnrng,XLOOKUP(rateType,rateNameRng,retRng)),  

b, 
IFERROR(XLOOKUP(TRIM(SUBSTITUTE(MID(SUBSTITUTE(","&tfl&REPT(" ",6),",",REPT(",",255)),2*255,255),",","")),tnrng,XLOOKUP(rateType,rateNameRng,retRng)),0),  

c, 
IFERROR(XLOOKUP(TRIM(MID(tfl,FIND("#",SUBSTITUTE(tfl,",","#",2))+1,255)),tnrng,XLOOKUP(rateType,rateNameRng,retRng)),0),  

a+b+c))

Prefer no VBA if possible. Using Win10, Excel 365 Monthly Enterprise channel.

braX
  • 11,506
  • 5
  • 20
  • 33

1 Answers1

1

Use SUMIFS with TEXTSPLIT:

=LAMBDA(TaskFormula,TaskNamesRange,ReturnRange,SUM(SUMIFS(ReturnRange,TaskNamesRange,TEXTSPLIT(TaskFormula,", "))))

enter image description here


TEXTSPLIT may not currently be available to all. In that case use FILTERXML in its place:

=LAMBDA(TaskFormula,TaskNamesRange,ReturnRange,SUM(SUMIFS(ReturnRange,TaskNamesRange,FILTERXML("<t><s>"&SUBSTITUTE(TaskFormula,",","</s><s>")&"</s></t>","//s"))))

enter image description here


Or this one that does not split the formula but looks to see if the item is included in the formula:

=LAMBDA(TaskFormula,TaskNamesRange,ReturnRange,SUM(BYCOL(SEQUENCE(,COLUMNS(TaskNamesRange),1,1),LAMBDA(a,IF(ISNUMBER(SEARCH(INDEX(TaskNamesRange,,a),TaskFormula)),INDEX(ReturnRange,,a),0)))))

enter image description here

Scott Craner
  • 148,073
  • 10
  • 49
  • 81
  • Thanks Scott! I'll try this out. TEXTSPLIT() is not available for me just yet so thanks for the other options. I'll try them out this week. – Ben Bennett Aug 28 '22 at 00:59