Answers:
基于我对您的问题的理解,我建议采用以下基于VBA的解决方案。
假设 - Sheet1具有excel列A到T中的数据表。表列名称通过G映射到实际的excel列E到K. Sheet2在单元格范围D4到E12中具有ShapeCode查找表。 “计算”选项设置为“自动”,这是Excel中的默认设置。
在Sheet1中按ALT + F11。 VBA代码编辑器将打开。从插入菜单插入模块。将插入名为Module1的模块(假设没有其他模块存在)并且名称将出现在左窗格中。主要是它还应该打开Module1的代码编辑器窗口,如果没有,双击Module1打开VBA代码编辑器。这里通过以下代码。
Function EvaluateExp(parm1 As String, parm2 As Long, parm3 As Long)
' parm1 is the VLOOKUP String
' parm2 is the row number
' parm3 is the column number of the first column in the table A thru G
Application.Volatile
Dim finalstring As String
finalstring = ""
For I = 1 To Len(parm1)
If I Mod 2 = 0 Then ' it's even position
Select Case Mid(parm1, I, 1)
' Ensure only + opertor is allowed. In future add more Case statements
' below in case more math operators are applicable
Case "+"
finalstring = finalstring & Mid(parm1, I, 1)
Case Else
EvaluateExp = CVErr(xlErrValue)
Exit Function
End Select
Else ' it's odd position
J = UCase(Mid(parm1, I, 1))
' Construct the expression from the actual columns mapping to A thru G
' In future if you insert more columns after G, add new Case statements here below
Select Case J
Case "A"
finalstring = finalstring & Cells(parm2, parm3).Address
Case "B"
finalstring = finalstring & Cells(parm2, parm3 + 1).Address
Case "C"
finalstring = finalstring & Cells(parm2, parm3 + 2).Address
Case "D"
finalstring = finalstring & Cells(parm2, parm3 + 3).Address
Case "E"
finalstring = finalstring & Cells(parm2, parm3 + 4).Address
Case "F"
finalstring = finalstring & Cells(parm2, parm3 + 5).Address
Case "G"
finalstring = finalstring & Cells(parm2, parm3 + 6).Address
Case Else
EvaluateExp = CVErr(xlErrValue)
Exit Function
End Select
End If
Next I
EvaluateExp = Application.Caller.Worksheet.Evaluate(finalstring)
End Function
此代码创建一个名为EvaluateExp的自定义函数,该函数包含3个参数。输入字符串,行号和列号。此函数分别将表列A到G映射到Excel列E到K,并构造预期的Excel表达式,包含在Evaluate中返回实际结果。
在单元格O3中的Sheet1中,输入围绕此自定义函数构建的以下公式,然后沿着表格的所需行向下拖动它。
=IF(ISBLANK(T3),"",EvaluateExp(VLOOKUP(T3,Sheet2!$D$4:$E$12,2,FALSE),ROW(),COLUMN(E3)))
当您在单元格T3及更低版本中输入形状代码时,该公式将根据查找返回的表达式返回预期的适用列总和。
该公式应返回#VALUE!在以下情况中
•查找表中不存在查找值
•引用表达式中除A到G之外的列
•查找表中的表达式不完整/无效,例如A + B +或ABCD或A + BC
•包含除+之外的数学运算符的表达式A-C
限制
•该函数将表格列A到G映射到实际的Excel列E到K. 将来如果在G之后插入更多列,则需要更新代码以适应相同的代码。
用户定义函数的以下代码足以支持任何有效的数学表达式。如果无法计算表达式,则返回#VALUE!错误。如果省略括号,则应遵循记录的运算符优先级 这里 。提及表达式中的所有适用运算符,例如(2 * A)+(3 * B)而不是2A + 3B。
Function EvaluateExp(parm1 As String, parm2 As Long, parm3 As Long)
' parm1 is the VLOOKUP String
' parm2 is the row number
' parm3 is the column number of the first column in the table A thru G
Application.Volatile
Dim finalstring As String
finalstring = ""
For I = 1 To Len(parm1)
J = UCase(Mid(parm1, I, 1))
Select Case J
Case "A"
finalstring = finalstring & Cells(parm2, parm3).Address
Case "B"
finalstring = finalstring & Cells(parm2, parm3 + 1).Address
Case "C"
finalstring = finalstring & Cells(parm2, parm3 + 2).Address
Case "D"
finalstring = finalstring & Cells(parm2, parm3 + 3).Address
Case "E"
finalstring = finalstring & Cells(parm2, parm3 + 4).Address
Case "F"
finalstring = finalstring & Cells(parm2, parm3 + 5).Address
Case "G"
finalstring = finalstring & Cells(parm2, parm3 + 6).Address
Case Else
finalstring = finalstring & J
End Select
Next I
EvaluateExp = Application.Caller.Worksheet.Evaluate(finalstring)
End Function