0

How can I declare and then assign variables dynamically in an iterative loop. For example:

for i = i to some_number
    dim "prefix" & i
    "prefix" & i = 2 * i
next
0

1 Answer 1

0
Dim MyArray(5)

For x = 0 to 4
    MyArray(x) = 2 * (x + 1)
Next

For each thing in MyArray
    Wscript.echo thing
Next

For x = 0 to 4
     wscript.echo MyArray(x)
Next

If you don't know how many a dictionary is usually easier. It you have two or more items it is easier. From Help. https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dictionary-object

Dim d                   'Create a variable
Set d = CreateObject(Scripting.Dictionary)
d.Add "a", "Athens"     'Add some keys and items
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Also to make them from text.

MyArray1 = Split("This is a bunch of words", " ")    

For each thing in MyArray1
    Wscript.echo thing
Next
Sign up to request clarification or add additional context in comments.

1 Comment

This approach might work but the OPs request is dynamically assigning variables which would use the Duplicate approach of Execute and ExecuteGlobal. To be honest, there isn't a lot of information to go off in the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.