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.