I want to add dynamic userform controls like (labels, textboxs) on runtime, when userfrom activated with following order.
i want something like following
when userfrom activate, it needs to ask user number of fields, he /she want to insert ?
if user answer 7, then it need to add field in following order "3 columns order "
Label1 textbox1 Label2 textbox1 Label3 textbox3
Label4 textbox4 Label5 textbox5 Label6 textbox6
Label7 textbox7 ........and so on based on number of field user ask.
i tried following codes:
i want something like following
when userfrom activate, it needs to ask user number of fields, he /she want to insert ?
if user answer 7, then it need to add field in following order "3 columns order "
Label1 textbox1 Label2 textbox1 Label3 textbox3
Label4 textbox4 Label5 textbox5 Label6 textbox6
Label7 textbox7 ........and so on based on number of field user ask.
i tried following codes:
VBA Code:
Private Sub UserForm_Initialize()
Dim i As Long
number = 10 'InputBox("Enter no of text-boxes and labels you wish to create at run-time", "Enter TextBox & Label Number")
Dim txtB1 As control
For i = 1 To 5
Set txtB1 = Controls.Add("Forms.TextBox.1")
With txtB1
.Name = "txtBox" & i
.Height = 20
.Width = 50
.Left = 70
.Top = 20 * i * 1
End With
Next i
For i = 6 To 10
Set txtB1 = Controls.Add("Forms.TextBox.1")
With txtB1
.Name = "txtBox" & i
.Height = 20
.Width = 50
.Left = 200
.Top = 20 * i - 100 * 1
End With
Next i
Dim lblL1 As control
For i = 1 To 5
Set lblL1 = Controls.Add("Forms.Label.1")
With lblL1
.Caption = "Label" & i
.Name = "lbl" & i
.Height = 20
.Width = 50
.Left = 20
.Top = 20 * i * 1
End With
Next i
For i = 6 To 10
Set lblL1 = Controls.Add("Forms.Label.1")
With lblL1
.Caption = "Label" & i
.Name = "lbl" & i
.Height = 20
.Width = 50
.Left = 150
.Top = 20 * i - 100 * 1
End With
Next i
Dim q As Long
For q = 1 To 5
Controls("lbl" & q) = Cells(1, q)
Next q
For q = 6 To 10
Controls("lbl" & q) = Cells(1, q)
Next q
End Sub