Solved

variable name substitution

Posted on 2001-06-22
6
284 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

707 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