variable name substitution

I have the following code.  This example is actually from VBA, but the question is a general VB one.

For x=1 to NameCounter
NameRef="plant" + trim(str(x)) + "name"
set NameRef = Range(Cells(begindatarow,namecol),Cells(enddatarow,namecol))

I need to modify this code so that the range in the Set statement is assigned to the value of the variable NameRef and not "NameRef" literally.
For example: plant1name=Range(Cells(begindatarow,namecol),Cells(enddatarow,namecol))

Who is Participating?
vindevogelConnect With a Mentor Commented:
Laura, it is possible.

You have to use the scripting run time.
Then you can have the content of a "plant" and not the word "plant" itself.  Like the evaluate function in FoxPro.

I wrote a wrapper class around it, stuffed it into a dll and it works.

This is the code of the class inside the dll
(I have one hidden form with the script control on it)

Private mScript As ScriptControl

Private Sub Class_Initialize()
    Load frmContainer
    Set mScript = frmContainer.ScriptControl
End Sub

Private Sub Class_Terminate()
    Set mScript = Nothing
    Unload frmContainer
End Sub

Public Sub Add(Code As String)
    mScript.AddCode Code
End Sub

Public Function Eval(VarCode As String) As Variant
    Eval = mScript.Eval(VarCode)
End Function

And this is code for a form to show you what it does ...

Private mVars As New Evaluate.Code

Private Sub Command1_Click()
    MsgBox mVars.Eval("command1.caption")
End Sub

I hope you have enough with this to get you going.
Otherwise, send a mail to
I'll send you the activeX component ....

Have a nice weekend !

I don't think it is possible(without stuff like API's orso) to create and set any variable value in code.

Can't you use a collection ?

Dim sht As Excel.Worksheet
Dim colNameRef As New Collection
Dim i As Integer

    Set sht = xl.Workbooks(1).Sheets(1)
    For i = 1 To 10
        colNameRef.Add sht.Range(Cells(i, 1), Cells(i, 1))
    Next i
End Sub

in this example xl is the reference to the Excel application containing the workbook

Have you already declared your variable names ie. plant1name, plant2name? If not your code wont work anyway.

You are probably better off defining an array of range objects and using those and using the NameCounter variable to resize the array eg.

Dim PlantNames() as Range    

Redim PlantNames(NameCounter)

For i = 1 to NameCounter
    Set PlantNames(i) = Range(Cells(begindatarow,namecol),Cells(enddatarow,namecol))
Next i

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Richie_SimonettiIT OperationsCommented:
Could you use name property of range object?
dim NameRef() as range
For x=1 to NameCounter
    redim preserve NameRef(x)
    NameRef(x).name ="plant" + trim(str(x)) + "name"
    set NameRef(x) = Range(Cells(begindatarow,namecol),Cells(enddatarow,namecol))

Hi laurag@devx,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept vindevogel's comment(s) as an answer.

laurag@devx, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer
Comment from expert accepted as expert

E-E Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.