Solved

variable name substitution

Posted on 2001-06-22
6
277 Views
Last Modified: 2012-08-13
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))
Next


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))

Thanks
0
Comment
Question by:laurag
6 Comments
 
LVL 12

Expert Comment

by:roverm
ID: 6219858
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

D'Mzzl!
RoverM
0
 
LVL 4

Expert Comment

by:mcmahon_s
ID: 6220586
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

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6220791
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))
Next


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Accepted Solution

by:
vindevogel earned 50 total points
ID: 6221266
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 yves@vindevogel.com
I'll send you the activeX component ....

Have a nice weekend !

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139710
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7174013
Comment from expert accepted as expert

Computer101
E-E Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now