Set variable name with string variable in VBA

2019-08-19 08:59发布

问题:

I have to create many variables inside a loop, so I need to create generic variable names (note, for the purpose of my code, i can't just append the objects i want to create to a list or collection)

The code I was thinking looks something like this (n is a user defined variable, and its an integer)

Dim var_name As String

For i = 1 To n
    var_name = "test" & i
    Set var_name = Range(Cells(1, 1), Cells(i * 2, i))
Next i

I would like to obtain variables like this:

  • test1 being a range from A1 to B2
  • test2 being a range from A1 to C4
  • test3 being a range from A1 to D6
  • .... testn being a range from A1 to whatever

They dont necesarily have to be range, all I really want is to declare a variable name using a string stored in another variable