vba equivilant of eval / execute

davestewart

New Member
Joined
Feb 15, 2006
Messages
43
Hi All,
Sorry if this has been asked before - I've tried searching the forum with no luck.

Is there a VB(A) equivilant?

I'd like to be able to such things as:

theSub="mySub"
Eval(theSub)

and a whole host of other things you would take for granted in other languages.

Thanks,
Dave
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Dave

It's not clear what you mean.

What do you want to evaluate/execute?
 
Upvote 0
I want to evaluate a string, and have it executed as if it were code.

For example, the following might pop up a MessageBox saying "London":

Code:
strVar1 = "Dave"
strVar2 = "London"
strVar3 = "Sunny"

n = 2

MsgBox Eval("strVar" & n)

Is this possible in VB?
 
Upvote 0
I'm not completely sure whatyou mean.... but I think

Function VBA_Evaluate(Input_String As String)

Application.Volatile

VBA_Evaluate = Application.Evaluate(Input_String)

End Function

Might be close. That evaluates the string as if it were entered in a cell, which is what the EVAL() function does.

However, the message box in the second post is just

msgbox strVar1
 
Upvote 0
OK - this is NOT an excel-speific question. It's about programming, and I want the answer to be VB-specific, not Excel-specific.

In other languages, Eval or Execute can be used to dynamically create code at run-time.

For example

Code:
strVal1 = "3"
strVal2 = "2"

Eval (strVal1 * strVal2)

' result is 6 (as Long)

See how the program has carried out the actions as if it were code? In the previous example, I was dynamically creating a reference to a variable:

Code:
Eval ("strVar" & n)

...to return a reference to the variable strVar2

You might use this appraoch as a dynamic alternative to using a Select Case


Any takers!?

Thanks,
Dave
 
Upvote 0
The equivalent of EVAL() is application.evaluate()

So

Code:
Sub Test()

a = "6"
b = "4"

n = application.evaluate(a)*Application.evaluate(b)

end sub

This code causes n to be 24. That's what you are trying to do, right?
 
Upvote 0
The only language that I use that supports a construct like Eval is JavaScript (and if one were to stretch the point, SQL). In any case, VB(A) doesn't support the execution of code created at run time.

If you have pre-defined subroutines, you could use the Run method of the Application object.

As far as taking for granted "a host of other things" goes, that's true for any language. I imagine VB(A) programmers take for granted features not available in other languages.
Hi All,
Sorry if this has been asked before - I've tried searching the forum with no luck.

Is there a VB(A) equivilant?

I'd like to be able to such things as:

theSub="mySub"
Eval(theSub)

and a whole host of other things you would take for granted in other languages.

Thanks,
Dave
 
Upvote 0
With regards to other languages: PHP, Javascript, Maxscript (to name but a few)

Check this code to see what I mean:

Code:
Sub Test()

    ' this code only works if there is a named range "myVar1"
    ' it references a cell, NOT a variable
    
    myVar1 = "Dave"
    myNum = 1
    myRef = "myVar" & myNum

    MsgBox "Checking variable contents: " & myVar1 ' will be "Dave"
    MsgBox "Building the string: " & myRef  ' will be "myVar1" ... the name of my variable
    
    newVar = Application.Evaluate(myRef) ' evaluate the string "myVar1" to get a reference to the variable "myVar1"
    
    MsgBox newVar  ' should be "Dave"

End Sub

I guess it's not possible, otherwise someone would know what I'm talking about.

So how do you VB guys dynamically create a reference to a variable? Or can't you?

Ta....



Cheers,
Dave
 
Upvote 0
Dave

What are you actually trying to do?

I was able to get that code to return Dave by going to Insert>Name>Define...
and entering myVar1 as the name and ="Dave" for refers to.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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