Solved

variable name substitution

Posted on 2001-06-22
6
276 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Accepted Solution

by:
vindevogel earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Comment from expert accepted as expert

Computer101
E-E Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now