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
1
  • No that's not possible in VBA; it doesn't have reflection. Actually not sure about reflection either... Commented Jan 6, 2016 at 10:02

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.