0

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.

(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:

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)

but not

=ev(Q25,10.000000001)

or even

=ev(Q25,10.00001)

Why is this happening? I need the program to calculate using small increments of up to 0.0000000001. Thank you for your insights.

16
  • Note: the string above might be wrong but i am not sure why SO prints it like that. I am certain that there are no errors or missing parenthesis in the formula Commented Oct 25, 2020 at 0:31
  • Text that is enclosed in * characters gets interpreted as italics. That's why your formula looked strange. If you escape the * character like this: \* then it won't create italics Commented Oct 25, 2020 at 0:50
  • Why don't you use A1 instead of x in your formula and simply change the value in A1 ? Commented Oct 25, 2020 at 1:03
  • 1
    I edited the question and add code tags, so the formula displays as intended. Commented Oct 25, 2020 at 1:04
  • 1
    I think Evaluate can only handle a string up to 255 characters long. Commented Oct 25, 2020 at 2:39

1 Answer 1

2

There is a 255 character limit to a String passed to Application.Evaluate. You're running into that hard limit after Replaceing x with the corresponding numeric value.

Your options include:

  • Breaking up the formula into pieces and Evaluateing it in several steps.
  • 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).
  • Using an approach like this and writing the revised formula to a cell, then reading back the calculated value.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.