1

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?

Scott Conover
  • 1,421
  • 1
  • 14
  • 27
Overseer10
  • 361
  • 2
  • 9
  • 20
  • 2
    I don't know if it's possible, but I know that variable variables are one of those rare cases that are absolutely positively nothing but pointless and harmful. Use a freaking dictionary instead. –  Jan 23 '11 at 22:33

2 Answers2

3

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
Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • So, then next question... is there Dictionary/Hash support in VBA? While it sounds like an Array/List is appropriate here, a Dictionary is a general-replacement for a "variable variables". –  Jan 23 '11 at 22:32
  • See http://stackoverflow.com/questions/1309689/hash-table-associative-array-in-vba – ThiefMaster Jan 23 '11 at 22:33
1

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!

Community
  • 1
  • 1
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190