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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Loop Variables

Status
Not open for further replies.

Janzer

Programmer
Jun 14, 2008
1
US
I need to be able to programtically loop through a project's variables. For example
Project1:
Private sub test()
myvariable =inputbox("boolean variable name to set?")
for i = 1 to 99
if myvariable = project2.variable then
project2.variable=True
exit for
endif
next
End sub
This is not the code to make it work but how I would like to approach it. Any ideas? Thank you!!
 




For starters...
Code:
Sub test()
    Dim obj
    For Each obj In Application.VBE.ActiveVBProject.VBComponents
        msgbox obj.name
    Next
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
What do you mean by 'myvariable = project2.variable'? Why do you loop 99 times?
If you need to test if variable with given name exists in Project2 just ask for typename, if you get an answer, variable/object exists (as public).
You need a reference to Project2 in Project1.

combo
 
I agree. You need to explain the 99 time thing. Why?

Here is your code, properly posted in a code window, and indented.
Code:
Private sub test()
myvariable = Inputbox("boolean variable name to set?")
  For i = 1 to 99
    If myvariable = project2.variable Then 
      project2.variable=True
      Exit For
    End If
  Next
End sub

As combo points out, unless porject2.variable is global, this routine will fail, as it will have no idea what is project2.variable. Either it has to be global, or added as a parameter.
Code:
Private sub test(otherVariable as Boolean)
myvariable = Inputbox("boolean variable name to set?")
  For i = 1 to 99
    If myvariable = otherVariable Then 
      otherVariable = True
      Exit For
    End If
  Next
End sub

But even the code above has problems unless that other variable is global, as you are not declaring it within the routine.

Further, myvariable (which is also not declared) is taking its value as a string from the inputbox. Your even use the word "name". That string is a string NOT a boolean value (True/False).



faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top