简体   繁体   English

Excel VBA 的求值函数中的#VALUE 错误

[英]#VALUE Error in Evaluate Function of Excel VBA

I am trying to find out what is wrong with the process below.我试图找出下面的过程有什么问题。 In my sheet, there is a formula string which needs evaluation for different values of variable, x .在我的工作表中,有一个公式字符串需要对变量x 的不同值进行评估。

(0.5*(23-9.81)*3*(x^2))*(x/3)+(0.5*9.81*(x^2))*(x/3)-1*((0.5*18.5*0.33*(2.5^2))*(x+2.5/3)+(18.5*0.33*2.5*x)*(x/2)+(0.5*(23-9.81)*0.33*(x^2))*(x/3)+(0.5*9.81*(x^2))*(x/3)+(100*0.33*(2.5+x))*((2.5+x)/2))

I have a vba code which looks like this:我有一个看起来像这样的 vba 代码:

Public Function ev(r As Range, x As Double) As Variant
    ev = Application.Evaluate(Replace(r.Value, "x", x))
End Function

What's weird is that this function only works if the cell input is like奇怪的是,此功能仅在单元格输入类似于

=ev(Q25,10.01) =ev(Q25,10.01)

but not但不是

=ev(Q25,10.000000001) =ev(Q25,10.000000001)

or even甚至

=ev(Q25,10.00001) =ev(Q25,10.00001)

Why is this happening?为什么会这样? I need the program to calculate using small increments of up to 0.0000000001 .我需要该程序使用高达0.0000000001小增量进行计算。 Thank you for your insights.谢谢你的见解。

There is a 255 character limit to a String passed to Application.Evaluate .传递给Application.EvaluateString有 255 个字符的限制。 You're running into that hard limit after Replace ing x with the corresponding numeric value.Replace ing x Replace为相应的数值后,您遇到了硬限制。

Your options include:您的选择包括:

  • Breaking up the formula into pieces and Evaluate ing it in several steps.将公式分解成多个部分并分几个步骤对其进行Evaluate
  • Rewriting the formula to something shorter as mentioned in the comments (though you may still surpass the 255 limit based on length of the proposed arguments).将公式重写为评论中提到的更短的内容(尽管根据建议的参数长度,您可能仍会超过 255 限制)。
  • Using an approach like this and writing the revised formula to a cell, then reading back the calculated value.使用类似的方法和写入修改后的公式的单元格,然后回读的计算值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM