VB- Runtime Calc. of An Expression with User Defined Func.

bomvoo

New Member
Joined
Nov 16, 2004
Messages
42
I´m trying to make a program to calculate in run-time a numerical expression entered as a string. I am successful when the numerical expression contains only built-in VBA functions.

I would be grateful to get some information on how should I do to get an expression containing user defined functions to be calculated in runtime.

To clarify any doubts on the question, below are two examples. The first works, the second doesn´t, how should I do to get the second to work?

Thanks! :biggrin:

'**********************EXAMPLE 1 **********************
Sub working_example()

'setting input variable
X1 = 10
'string defining expression to be evaluated
f_of_x = "x^2+2*x+3"

Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
sc.Reset
sc.ExecuteStatement ("x = " & X1) 'Execute statement for x variable = X1
result = sc.Eval(f_of_x) 'Evaluate it

'this works and give result=123

End Sub

'**********************EXAMPLE 2 **********************

Sub not_working_example()

'setting input variable
X1 = 10
'string defining expression to be evaluated - This does not work
f_of_x = "operation(x)"

Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
sc.Reset
sc.ExecuteStatement ("x = " & X1) 'Execute statement for x variable = X1
result = sc.Eval(f_of_x) 'Evaluate it

'this does not work because f_of_x refers to a user defined function -operation-
'the question is how to include in a string expression a user defined function and
'get the expression calculated in run-time.
End Sub

************FUNCTION USED IN EXAMPLE 2 **********************

Function operation(x)

operation = x ^ 2 + 2 * x + 3

End Function
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
These worked for me:

Code:
Sub T1()
    f_of_x = "operation"
    x = 10
    result = Evaluate(f_of_x & "(" & x & ")")
    MsgBox result
End Sub

Sub T2()
    f_of_x = "operation()"
    x = 10
    result = Evaluate(Replace(f_of_x, "()", "(" & x & ")"))
    MsgBox result
End Sub
 

Forum statistics

Threads
1,137,294
Messages
5,680,660
Members
419,923
Latest member
Kalthus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top