ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looping with incremental variable/object names (https://www.excelbanter.com/excel-programming/382160-looping-incremental-variable-object-names.html)

Charles Hamlyn

Looping with incremental variable/object names
 
right now I have code similar to below:

Variable1 = Form1.TextboxWidth1 * Form1.TextboxLength1
Variable2 = Form1.TextboxWidth2 * Form1.TextboxLength2
Variable3 = Form1.TextboxWidth3 * Form1.TextboxLength3
Variable4 = Form1.TextboxWidth4 * Form1.TextboxLength4
Variable5 = Form1.TextboxWidth5 * Form1.TextboxLength5
....

It's more complicated than that, but you get the idea. So anyway, I'd like
to make a loop to accomplish the same task. Something along the lines of:

For LoopCounter = 1 to 4
NewVariableName = "Variable" & LoopCounter
NewTextWidthName = "Form1.TextBoxWidth" & LoopCounter
NewTextLengthName = "Form1.TextBoxLength" & LoopCounter

NewVariableName = NewTextWidthName * NewTextLengthName
Next LoopCounter

I'm pretty sure I've figured out how to do this in the past but I can't find
anything about it now. How can I accomplish this? Is there a better way
I'm missing?

Thank you for any assistance you can give.
Charles Hamlyn



Charles Hamlyn

Looping with incremental variable/object names
 
Personal E-mail from Edward R. Galimi

Charles,


You can do a for. each. next statement when working with objects.


Regards,
Eddie


right now I have code similar to below:

Variable1 = Form1.TextboxWidth1 * Form1.TextboxLength1
Variable2 = Form1.TextboxWidth2 * Form1.TextboxLength2
Variable3 = Form1.TextboxWidth3 * Form1.TextboxLength3
Variable4 = Form1.TextboxWidth4 * Form1.TextboxLength4
Variable5 = Form1.TextboxWidth5 * Form1.TextboxLength5
...

It's more complicated than that, but you get the idea. So anyway, I'd
like to make a loop to accomplish the same task. Something along the
lines of:

For LoopCounter = 1 to 4
NewVariableName = "Variable" & LoopCounter
NewTextWidthName = "Form1.TextBoxWidth" & LoopCounter
NewTextLengthName = "Form1.TextBoxLength" & LoopCounter

NewVariableName = NewTextWidthName * NewTextLengthName
Next LoopCounter

I'm pretty sure I've figured out how to do this in the past but I can't
find anything about it now. How can I accomplish this? Is there a better
way I'm missing?

Thank you for any assistance you can give.
Charles Hamlyn




Tom Ogilvy

Looping with incremental variable/object names
 
What Galimi says is true, but not sure how that figures in here since any
loop goes through each control sequentially. I suppose you could parse the
number out of the name and do something, but for your specific example,
probably easier to do:

Assuming form1 is the name of a userform:

dim variables(1 to 5) as double

for i = 1 to 5
variable(i) = form1.Controls("TextboxWidth" & i).Value * _
form1.Controls("TextboxLength" & i).Value
Next i

If form1 is not a userform, then post back with what it is.

--
Regards,
Tom Ogilvy



"Charles Hamlyn" wrote:

Personal E-mail from Edward R. Galimi

Charles,


You can do a for. each. next statement when working with objects.


Regards,
Eddie


right now I have code similar to below:

Variable1 = Form1.TextboxWidth1 * Form1.TextboxLength1
Variable2 = Form1.TextboxWidth2 * Form1.TextboxLength2
Variable3 = Form1.TextboxWidth3 * Form1.TextboxLength3
Variable4 = Form1.TextboxWidth4 * Form1.TextboxLength4
Variable5 = Form1.TextboxWidth5 * Form1.TextboxLength5
...

It's more complicated than that, but you get the idea. So anyway, I'd
like to make a loop to accomplish the same task. Something along the
lines of:

For LoopCounter = 1 to 4
NewVariableName = "Variable" & LoopCounter
NewTextWidthName = "Form1.TextBoxWidth" & LoopCounter
NewTextLengthName = "Form1.TextBoxLength" & LoopCounter

NewVariableName = NewTextWidthName * NewTextLengthName
Next LoopCounter

I'm pretty sure I've figured out how to do this in the past but I can't
find anything about it now. How can I accomplish this? Is there a better
way I'm missing?

Thank you for any assistance you can give.
Charles Hamlyn





Charles Hamlyn

Looping with incremental variable/object names
 
Right, but can you do it without saying "For each textbox on Form1..."
because I need to access specific numbered objects at different times.

All I'm looking to do is build the name of an object and/or the name of a
variable into a 2nd variable. Then have VBA use the VALUE of that variable
as the object's name.

Thanks
Charles



"Charles Hamlyn" wrote in message
...
Personal E-mail from Edward R. Galimi

Charles,


You can do a for. each. next statement when working with objects.


Regards,
Eddie


right now I have code similar to below:

Variable1 = Form1.TextboxWidth1 * Form1.TextboxLength1
Variable2 = Form1.TextboxWidth2 * Form1.TextboxLength2
Variable3 = Form1.TextboxWidth3 * Form1.TextboxLength3
Variable4 = Form1.TextboxWidth4 * Form1.TextboxLength4
Variable5 = Form1.TextboxWidth5 * Form1.TextboxLength5
...

It's more complicated than that, but you get the idea. So anyway, I'd
like to make a loop to accomplish the same task. Something along the
lines of:

For LoopCounter = 1 to 4
NewVariableName = "Variable" & LoopCounter
NewTextWidthName = "Form1.TextBoxWidth" & LoopCounter
NewTextLengthName = "Form1.TextBoxLength" & LoopCounter

NewVariableName = NewTextWidthName * NewTextLengthName
Next LoopCounter

I'm pretty sure I've figured out how to do this in the past but I can't
find anything about it now. How can I accomplish this? Is there a
better way I'm missing?

Thank you for any assistance you can give.
Charles Hamlyn







All times are GMT +1. The time now is 02:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com