Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question: Executing a String as a statement 2

Status
Not open for further replies.

jayjayv

Technical User
Jan 1, 2003
17
AP
Hi,

I am new to Excel VBA,

Question: Is there a way I can execute the string "temp" as a statement ?

eg:
dim temp as string
temp = "Qserform1.combobox1.Clear"




Thanx in Advance
Jay
 
Not sure what you are trying to do but if it is just to clear a combobox then
Qserform1.combobox1.Clear

should work without loading it into a string.
 
I am pretty sure it will not work AS a string. In VBA instructions are parsed at run-time, it is true, as text, therefore strings. But strings themselves are used either as variables, or parsed as literals. That is why they are passed into the compiler as literals OR varaiables. Instructions themselves are not.

I would be quite happy to be corrected.
jayjayv - did you actually TRY to make this doing anything?

Gerry
 
I was infact trying to create a for loop as below
dim temp as string

for i = 1 to 10
temp= "lable" & i & "Caption"
XXXXX(temp)= i
next

I was looking for the "XXXXX" function that woud allow me to write smaller code with a lots of less typing to do !!! :)

Any idea if there are any such functions in VB.

Any suggestions Pls.

Thanx in Advance
Jay
 
Sure. Here's an example. You'll need a user form with a combobox and two command buttons. You'll also need a reference to the Microsoft Script Control 1.0. Then jut copy and paste the following code into the form:
Code:
[blue]Option Explicit
Private MyScript As ScriptControl

Private Sub CommandButton1_Click()
    Set MyScript = New ScriptControl
    MyScript.Language = "vbscript"
    MyScript.AddObject "userform1", UserForm1, True
    UserForm1.ComboBox1.AddItem "hello2"
End Sub

Private Sub CommandButton2_Click()
    Dim temp As String
    temp = "Userform1.combobox1.Clear"

    MyScript.ExecuteStatement temp
End Sub[/blue]
 
Hi jayjayv,

If what you want to do is reference controls on a form, and they are all named according to a standard, you can do something like this ..

Code:
For i = 1 To 10
    QserForm1.Controls("Label" & i).Caption = "whatever " & i
Next

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top