Solved

variable name substitution

Posted on 2001-06-22
6
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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