ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Assigning the name of a userform/control to a changing string (https://www.excelbanter.com/excel-programming/271752-assigning-name-userform-control-changing-string.html)

Brad Patterson

Assigning the name of a userform/control to a changing string
 
I want to attach a name to a userform (i.e. userform1,2,3 etc) or a control
within a form (i.e. TextBox1,2,3 etc) by means of a loop. How do I name the
userform/control with a variable such as a string ...

Dim UF as UserForm
Dim str as string
Dim j as Integer

For j = 1 to 5
str = ³UserForm² & j
UF.Name = str
UF.Show
Next j

- It doesn¹t work, but is it close?

Thanks for any help.


Leo Heuser[_2_]

Assigning the name of a userform/control to a changing string
 
Hi Brad

What you want can only be accomplished, if the userform/control
is created at run time. It's not possible to alter a name given at design
time programmically.

This example shows the basics:

Private Sub UserForm_Initialize()
Dim Counter As Long

For Counter = 1 To 3
Me.Controls.Add "forms.checkbox.1", Name:="Burger" & Counter
Next Counter

Me.Controls("Burger2").Value = True ' or Me!burger2.Value = True

MsgBox Me!burger2.Value
End Sub



--
Best Regards
Leo Heuser
MVP Excel

Followup to newsgroup only, please.

"Brad Patterson" skrev i en meddelelse
...
I want to attach a name to a userform (i.e. userform1,2,3 etc) or a control
within a form (i.e. TextBox1,2,3 etc) by means of a loop. How do I name the
userform/control with a variable such as a string ...

Dim UF as UserForm
Dim str as string
Dim j as Integer

For j = 1 to 5
str = "UserForm" & j
UF.Name = str
UF.Show
Next j

- It doesn't work, but is it close?

Thanks for any help.




Brad Patterson

Assigning the name of a userform/control to a changing string
 
Thanks leo, but I wanted to name the item iteratively * when they already
exist.

It¹s just trying to refer to the textbox with a string that had me stumped,
but I know the answer thanks to your post and the Me.Control(²textbox
....²).value command.

Thanks!


in article , Leo Heuser at
wrote on 16/7/03 6:37 PM:

Private Sub UserForm_Initialize()
Dim Counter As Long

For Counter = 1 To 3
Me.Controls.Add "forms.checkbox.1", Name:="Burger" & Counter
Next Counter

Me.Controls("Burger2").Value = True ' or Me!burger2.Value = True

MsgBox Me!burger2.Value
End Sub






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

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