Is Variable Variable Possible?

2019-08-06 02:02发布

问题:

My googlefu has failed me and I come to you for help:

Is VBA capable of having Variable Variables like PHP? I know that in PHP you can wait to declare a variable by using $$var.

Is it possible to do it in VBA? for example, is there a way that lets say: I read an entire array of 1000 strings and each string that I get can declare a variable with that string, e.g if the 80th element of an array is named STO how can I tell VBA to create a variable with the name sto?

回答1:

It's not possible. But almost any code which relies on variable variables is horribly broken anyway and should be refactored e.g. to use array.

Edit (pst): If you need to access values by a given name a dictionary can be used. An excerpt/example:

Dim d As dictionary
Set d = New dictionary
d("STO") = arr(80) 'or whatever it is in VBA


回答2:

Well you can ... sort of.

By manipulating the VBE you may add lines to your program, so defining variables as you want. Of course this is not a straightforward way, nor I recommend it. It's just for your info.

Here is a tutorial.

More examples can be found googling "VBE insertlines"

HTH!