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

B

Brad Patterson

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.
 
L

Leo Heuser

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" <[email protected]> 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.
 
B

Brad Patterson

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top