 |
pavane Level: VB Lord
 Registered: 26-04-2004 Posts: 179
|
Expression evaluator
I need an expression evaluator routine, callable from VBA, which will evaluate an arbitrary mathematical expression input by a user, including common math functions. Does anyone know how I could do this (without writing my own, of course).
|
|
06-12-2006 at 04:53 PM |
|
|
pavane Level: VB Lord
 Registered: 26-04-2004 Posts: 179
|
Re: Expression evaluator
Just to let you know that I have solved the problem myself.
Provided that the expression or formula contains only literal values and Excel function names, you can let Excel do the work in the following way:
1. Within VBA, Create a temporary Excel worksheet
2. Save the formula to a cell's Formula object
3. Read the result from the same cell's Value object.
Example: if you save "=(1+2+3)" to the formula, then you can read the result (6) from the cell's value.
You could also use excel functions in the formula, as for example 'exp' in the following, "=(1+2+4+exp(4))"
As an extra, you can preload the cell with the text ERROR, and that will be returned if your formula is incorrect in structure.
|
|
10-01-2007 at 06:25 PM |
|
|
pavane Level: VB Lord
 Registered: 26-04-2004 Posts: 179
|
Re: Expression evaluator
Just a little extra note.
Excel DOES object if a formula ends with an operator, like + or -, which can happen if the last value is null.
To avoid this, you can end each expression with +0, which obviously has no effect on the result.
|
|
11-02-2007 at 03:44 PM |
|
|
|
|
 |
 |