问题
I have a formula that works perfectly when used in a With / End With statement for the range:
.Formula = "=IFERROR(INDEX(" & MasterDataRange.Address(External:=True) & ",MATCH(" & Cells(iRow, Entry1).Address(True, False) & "&Left(" & Cells(iRow + 1, Entry2).Address(False, False) & ", 4)&""" & wks4.Range("L7").Value & """," & MasterRowMatchRange.Address(External:=True) & ",0),MATCH(""" & header01 & """," & MasterColumnMatchRange.Address(External:=True) & ",0)),0)"
When trying to Evaluate the above and assign it to a Variant, the Variant returns an error result 'Error 2015'.
xResult = Evaluate("=IFERROR(INDEX(" & MasterDataRange.Address(External:=True) & ",MATCH(" & Cells(iRow, Entry1).Address(True, False) & "&Left(" & Cells(iRow + 1, Entry2).Address(False, False) & ", 4)&""" & wks4.Range("L7").Value & """," & MasterRowMatchRange.Address(External:=True) & ",0),MATCH(""" & header01 & """," & MasterColumnMatchRange.Address(External:=True) & ",0)),0)")
Can someone help me understand this? Thanks in advance.
回答1:
After a lot of digging around, it turns out that there's a 255 character limit to the vba Evaluation Method, plus there I needed to remove some string references utilised by the .Formula Method
Thanks for the help. For those interested, the code is now:
Set cell = wks4.Range("L7")
On Error Resume Next
rResult = Evaluate("MATCH(" & Cells(iRow, Entry1).Address(True, False) & "&" & "Left(" & Cells(iRow + 1, Entry2).Address(False, False) & ", 4)" & "&" & cell.Address(External:=True) & "," & MasterRowMatchRange.Address(External:=True) & ",0)") 'Match Header
cResult = Evaluate("MATCH(""" & header01 & """," & MasterColumnMatchRange.Address(External:=True) & ",0)") 'Match Column
xResult = Evaluate("INDEX(" & MasterDataRange.Address(External:=True) & "," & rResult & "," & cResult & ")")
On Error GoTo 0
If IsError(xResult) Then 'Handle Error 2042 = #N/A
Range(Cells(iRow, iCol), Cells(iRow, iCol)) = 0
Else:
Range(Cells(iRow, iCol), Cells(iRow, iCol)) = xResult
End If
回答2:
In some cases you can't get around the 255 character limit of formulae without writing the formulaes directly to cells. Excel's newer evaluation engine doesn't have the same limitations that the old one does (the one which is used by Application.Evaluate
. A working prototype of a function which evaluates >255 char limit is as follows:
Function EvaluateAny(ByVal sFormula as string) as variant
static evaluatorCell as Range: if evaluatorCell is nothing then set evaluatorCell = Range("...")
if len(sFormula) > 255 then
evaluatorCell.formula = sFormula
evaluatorCell.Calculate
EvaluateAny = evaluatorCell.value
else
EvaluateAny = Application.Evaluate(sFormula)
end if
End Function
Note: This method will be significantly slower than Application.Evaluate()
Another alternative would be to use stdLambda so you have a more direct implementation of your formula:
Dim func as stdLambda: set func = stdLambda.Create("$1 > 10")
Debug.Print func(100) 'true
Debug.Print func(5) 'false
You'll need to include stdICallable interface if you go down this route.
来源:https://stackoverflow.com/questions/60599688/how-do-i-write-this-evaluate-formula-so-that-the-variant-doesnt-return-an-error