borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Listview)Next Topic (real time mschart with scrollbar) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Expression evaluator Solved Topic
Poster Message
pavane
Level: VB Lord

Registered: 26-04-2004
Posts: 179

icon 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
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
pavane
Level: VB Lord

Registered: 26-04-2004
Posts: 179
icon 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
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
misterxed
Level: VB Lord


Registered: 12-06-2005
Posts: 151
icon Re: Expression evaluator

Thanx for sharing the workaround!

____________________________
lOsT...

14-01-2007 at 01:26 PM
View Profile Send Email to User Show All Posts | Quote Reply
pavane
Level: VB Lord

Registered: 26-04-2004
Posts: 179
icon 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
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : VB General : Expression evaluator Solved Topic
Previous Topic (Listview)Next Topic (real time mschart with scrollbar) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder