Is Variable Variable Possible?

痞子三分冷 提交于 2019-12-11 05:12:41

问题


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!



来源:https://stackoverflow.com/questions/4777001/is-variable-variable-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!