1

Is naming of variables with composition of text and another variables possible?

An example (not functional):

Components = "AA,BB,CC"
Values = "101,102,103"
ComponentsArr = Split(Components)
ValuesArr = Split(Values)
For i = 1 To UBound(ComponentsArr)
    Let "Var" & ComponentsArr(i) = ValuesArr(i)
Next i
Community
  • 1
  • 1
marmou
  • 11
  • 2
  • No that's not possible in VBA; it doesn't have reflection. Actually not sure about reflection either... – D_Bester Jan 06 '16 at 10:02

2 Answers2

1

You might be able to use a dictionary to accomplish your purpose. You add key/value pairs to the dictionary. Then you can use the key to get the value.

Set a reference to MS Scripting runtime ('Microsoft Scripting Runtime')

Components = "AA,BB,CC"
Values = "101,102,103"
ComponentsArr = Split(Components, ",")
ValuesArr = Split(Values, ",")

Dim dict As New Scripting.Dictionary
For i = LBound(ComponentsArr) To UBound(ComponentsArr)
    Call dict.Add(ComponentsArr(i), ValuesArr(i))
Next i

If dict.Exists("AA") Then
    Debug.Print dict.Item("AA") 'prints 101
End If

See also: https://stackoverflow.com/a/915333/2559297

Community
  • 1
  • 1
D_Bester
  • 5,723
  • 5
  • 35
  • 77
1

It might be easier to do it like the following:

Components = Array("AA", "BB", "CC")
Values = Array("101", "102", "103")

Then you wouldn't need ComponentsArr and ValuesArr.

For i = LBound(Components) To UBound(Components)
    dict.Add(Components(i), Values(i))
Next i
Kathara
  • 1,226
  • 1
  • 12
  • 36
  • Sure that works great. I took the first four lines as-is and focused on the dictionary aspect. Focusing on one idea can be an effective teaching tool. – D_Bester Jan 13 '16 at 15:44