views:

360

answers:

1

Hi guys, I have a problem with some excel code that I am having trouble getting my head around.

Okay so I am using the application.evaluate command in excel vba, office 2007.

If i have Evaluate("SIN(45)") it returns a nice predicted number. However if I do Evaluate("eq") the code crashes.

eq is an equation i am reading in from excel. the equation is: 3*x^2+5*x+1. It is passed in as a string. to make sure this happened I placed it in another variable that I had defined as a string. I replace the x's in the equation using excel's replace function. equation = Replace(equation, "x", temp).

However, when i get to the evaluate the code breaks down and I am not sure why. Total = Total + Evaluate("equation"). Any help is greatly appreciated

+3  A: 

I think the problem lies where you references a string "equation" instead of the equation string.

I would do

Evaluate(equation) or Evaluate(Replace(equation, "x", temp)) instead, note without the quotations " around equation and that would yield a valid answer.

Hope this help.

K2so
wow! that worked, thank you so much!
Samuel
Charles Williams has a nice description of some of the many quirks of using Application.Evaluate here: http://www.decisionmodels.com/calcsecretsh.htm
jtolle