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
 
Hi Norie...

Concept: Dynamically create a reference to an instantiated variable at run-time
Practice: None as yet. Just checking out VB.

As per the above example ... I don't want to reference a named range (although, this is very useful) I want a reference to a Dim'ed variable.

Does that make sense?

I know I could use arrays to simulate this for simple variables, but a lot of times it's useful to do it this way.

Any ideas?
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Is this what you are trying to do?

The macro called is now dependent on the random number.....

Code:
Sub Glove1()
Selection.Font.ColorIndex = 3
End Sub


Sub Glove2()
Selection.Font.ColorIndex = 5
End Sub

Sub RandomColour()

n = 1
If Rnd() > 0.5 Then n = 2

Application.Run("Glove" & n)

End Sub
 
Upvote 0
I don't know your background but to claim you can "simulate" what you want with arrays for "simple variables" leaves out a lot of capability in modern languages. You might want to look into user defined types, classes (and objects), and collections (and similar).

You may be surprised to learn that very few 3GL/4GLs support the concept of execution of dynamically created code -- with good reason. It makes a program very difficult to understand, debug, or maintain.
{snip}
I know I could use arrays to simulate this for simple variables, but a lot of times it's useful to do it this way.

Any ideas?
 
Upvote 0
Glove_Man - thanks, this is getting closer to what I need. Yor example errors on my machine, but the help looked promising. Still need to know to reference variables though.

tusharm - thank you for your response, even if it was a little condescending.

Cheers.
 
Upvote 0
You are trying to set a reference to a value type. Pre net VB does not represent primitive types as objects. Like Tusharm stated, you will need to create your own objects in order to do this. Nevertheless, VBA's CallByName Function may be in the direction you are looking.
 
Upvote 0
Dave,

I know exactly what you are talking about. It's been a decade or more since I programmed in DBase or Clipper, but as I recall it had a mechanism for doing this. In the thousands of threads I've read here, I have never seen it. And if Tushar and Tom, two of the best programmers around this joint say it ain't so, then, personally I wouldn't waste more time on it.

Back to what Norie said about ranges. It ain't pretty. But it does indeed work just like you're looking to do. Three named ranges {range1, range2 and range3}...
Code:
Sub TestEvaluate()
    Dim varMemory1, varMemory2, varMemory3
    
    varMemory1 = 11
    varMemory2 = 22
    varMemory3 = 33
    
    
    For i = 1 To 3
        Debug.Print "memory"; i, Application.Evaluate("varMemory" & i)
    Next i
    
    For i = 1 To 3
        Debug.Print "ranges"; i, Application.Evaluate("range" & i)
    Next i
    
End Sub
Produces the following output:
<pre>memory 1 Error 2029
memory 2 Error 2029
memory 3 Error 2029
ranges 1 111
ranges 2 222
ranges 3 333
</pre>
 
Upvote 0
FWIW -- what's wrong with a collection?
Code:
Sub TestCollection()
    Dim c As Collection
    Set c = New Collection
    
    c.Add 1.234, "varMemory11"
    c.Add #2/2/2006#, "varMemory22"
    c.Add "MrExcel", "varMemory33"
    
    For i = 11 To 33 Step 11
        Debug.Print "collection"; i, c.Item("varMemory" & i)
    Next i
    
    Set c = Nothing
End Sub
 
Upvote 0
Hi Greg,
Thanks for your reply :)

Yeah - the ranges stuff was new to me (I'm just picking up vb for 6 months or so for a personal project) and I think that functionality will get used at some point.

Collections - yes, that's what I said above: "I know I could use arrays to simulate this for simple variables, but a lot of times it's useful to do it this way."

The drawback to this is that the object has to be an element of a collection, and can't just be an arbitrary object / interface item / function / variable in my VB project.

Let's say I have a dropdown list, or an ActiveX treeview, and I select a value or click a node, which returns a value.

This value could be an integer (great for for collections) or it could be a string value such as "red". The great thing about eval() is that you can just take the value "red", concatenate it with another value (say "Image") and have the programming language return a reference the object "redImage"... to which you might then call a method upon:

Code:
var pic=eval("red" & "Image")
pic.resizeTo(50,50)

It just allows you to write very powerful code, and is not in any way similar to classes at all. If I wrote a class to achieve the same, it probably use a collection to do something similar.

At the last count I write 12 languages, and I think I know what I'm talking about ... and either VB has it or it doesn't ... and it looks like it doesn't!

Thank you everyone for the help. By all means keep it coming, and I hope to converse with one or two of you again.

Cheers,
Dave Stewart
Code:
 
Upvote 0
Dave

What type of object might redImage be?
 
Upvote 0
Who knows - an image on a form if you need to put a label on it.

The object is arbitrary - it could be anything: an interface item, a function, a menu, a class, etc, etc.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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