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

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
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
 
Upvote 0

Forum statistics

Threads
1,214,806
Messages
6,121,672
Members
449,045
Latest member
Marcus05

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
Back
Top