Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you interpret a String Value as a Variable Name

Status
Not open for further replies.

MrMeReturns

Technical User
Feb 11, 2003
6
CA
I want a string value to be interpreted as a variable name. For example

sTest = "i" & objControl.Name
dTest2 = CDbl(objControl.Value)
adImpericalData(sTest) = dTest2 'This is an array

""i" & objControl.Name" form the name of a constant array index. The way this code stands the value of the variable sTest is interpreted a simply a string - I want it to be interpreted as the name of the constant ""i" & objControl.Name" which refers to an array index value.

Any suggestions?
 
MrMeReturns,

It sounds like what you want is an associative array (what Perl users call a hash). What this does is sets up an array where the subscripts are strings instead of numbers.

According to the help pages in excel97 VBA, this is available as a 'Dictionary Object.' I have never used this, so I cannot give any helpful hints, but the help pages should get you started.

MM
 
I had a few extra minutes and did a little more digging...

First, you need to make sure you have added the 'Microsoft Scripting Runtime' library. You can do this through your References under "Tools".

Once this is done, I was able to run this snippet of code successfully:

Sub Play()

Dim var1 As String
Dim var2 As String
Dim int1 As Integer
Dim AssocArray As Dictionary

var1 = "Jelly"
var2 = "Stuff"
int1 = 35

Set AssocArray = New Dictionary

AssocArray.Item("e" & var1) = "peanut butter"
AssocArray.Item(var2 & "y") = "open a window"
AssocArray.Item("Age") = 35

MsgBox AssocArray.Item("eJelly"), vbOKOnly
MsgBox AssocArray.Item(var2 & "y"), vbOKOnly
MsgBox AssocArray.Item("Age"), vbOKOnly


End Sub

I think this is what you want to do...Let me know if this helps.
 
I have been trying your idea of using the 'Dictionary Object', it works pretty slick :)

Thanks for your help,

MrMeReturns
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top