我真的很难知道如何表达我想要的结果,但我会告诉你一个例子。
我正在使用Google表格。 我有包含多个参数的单元格。 例如: enter image description here
所以我想这样做: 如果它找到"项目名称"中的一个变量。在"项目编号"它将以匹配价格替换它,然后乘以数量,但按每个参数的位置顺序。
For example:
1st CELL: =PINK(should be $10)*1+Red(should be $12)*4+Blue(should be $46)*3
2nd CELL: =12*3+10*5 [FINAL REASULT]
我希望这是可以理解的......谢谢!
答案 0 :(得分:0)
我怀疑您可能想要调查VBA解决方案。 对于表格,我想出了这个公式:
=ArrayFormula(SUM(IF(ISNA(VLOOKUP(T(IF(1,TRIM(MID((SUBSTITUTE(SUBSTITUTE(A2," ",""),",",REPT(" ",99))),IF(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))=1,1,(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))-1)*99),99)))),PriceTbl,2,0)*TRIM(MID((SUBSTITUTE(SUBSTITUTE(B2," ",""),",",REPT(" ",99))),IF(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))=1,1,(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))-1)*99),99))),0,VLOOKUP(T(IF(1,TRIM(MID((SUBSTITUTE(SUBSTITUTE(A2," ",""),",",REPT(" ",99))),IF(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))=1,1,(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))-1)*99),99)))),PriceTbl,2,0)*TRIM(MID((SUBSTITUTE(SUBSTITUTE(B2," ",""),",",REPT(" ",99))),IF(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))=1,1,(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))-1)*99),99)))))
我对Sheets并不熟悉,但如果Sheets允许使用命名公式,那么公式可以略微缩短,就像Excel一样。
在Excel中,公式为数组公式(使用ctrl + shift + enter输入):
=SUM(IF(ISNA(VLOOKUP(T(IF(1,TRIM(MID((SUBSTITUTE(SUBSTITUTE(A7," ",""),",",REPT(" ",99))),seq_99,99)))),PriceTbl,2,0)*TRIM(MID((SUBSTITUTE(SUBSTITUTE(B7," ",""),",",REPT(" ",99))),seq_99,99))),0,VLOOKUP(T(IF(1,TRIM(MID((SUBSTITUTE(SUBSTITUTE(A7," ",""),",",REPT(" ",99))),seq_99,99)))),PriceTbl,2,0)*TRIM(MID((SUBSTITUTE(SUBSTITUTE(B7," ",""),",",REPT(" ",99))),seq_99,99))))
其中
seq_99 refers to: =IF(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))=1,1,(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))-1)*99)
而且,在我看来,更容易维护的是VBA解决方案:
Option Explicit
Function totalPrice(Items As String, Quantity As String, PriceTbl As Range) As Currency
Dim vI, vQ
Dim vPriceTbl
Dim I As Long, J As Long
Dim Price As Currency
vI = Split(Items, ",")
vQ = Split(Quantity, ",")
'if the price table is lengthy, using a dictionary to store the amounts
' with the key being the "color", would be much more efficient than
' looping to find the correct price
vPriceTbl = PriceTbl
For I = 0 To UBound(vI)
For J = 1 To UBound(vPriceTbl, 1)
If Trim(vI(I)) = vPriceTbl(J, 1) Then
Price = Price + vQ(I) * vPriceTbl(J, 2)
End If
Next J
Next I
totalPrice = Price
End Function