问题
I want to add dynamic userform controls like (labels, textboxes) 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.
Request for enhancement in code :
i tried following codes:
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
回答1:
Here's one way to do an unlimited number of controls in 3 columns. Instead of using nested For loops, try using a Do loop:
Option Explicit
Private Sub UserForm_Initialize()
Dim nof As Integer
nof = InputBox("Enter no of text-boxes and labels you wish to create at run-time", "Enter TextBox & Label Number")
Dim nob As Integer
nob = InputBox("Enter no of blanks you wish to create at run-time", "Enter Blanks")
CreateFields nof, nob
End Sub
Private Sub CreateFields(ByVal NumberOfFields As Integer, _
ByVal NumberOfBlankFields As Integer)
Dim i As Integer
Dim j As Integer
Dim Top As Single
Dim Left As Single
Top = 20
Left = 20
i = 1
j = 1
Do While i <= NumberOfFields + NumberOfBlankFields
If i > NumberOfBlankFields Then
CreateLabel "lbl" & j, "Label" & j, Top, Left
CreateTextBox "txtBox" & j, "Text" & j, Top, Left + 30
j = j + 1
End If
i = i + 1
If i Mod 3 = 1 Then
Top = Top + 50
Left = 20
Else
Left = Left + 100
End If
Loop
End Sub
Private Sub CreateLabel(ByVal Name As String, _
ByVal Caption As String, _
ByVal Top As Single, _
ByVal Left As Single)
With Controls.Add("Forms.Label.1")
.Name = Name
.Caption = Caption
.Height = 20
.Width = 50
.Left = Left
.Top = Top
End With
End Sub
Private Sub CreateTextBox(ByVal Name As String, _
ByVal Text As String, _
ByVal Top As Single, _
ByVal Left As Single)
With Controls.Add("Forms.TextBox.1")
.Name = Name
.Text = Text
.Height = 20
.Width = 50
.Left = Left
.Top = Top
End With
End Sub
Of course, you can adjust the spacing of the controls as needed.
来源:https://stackoverflow.com/questions/61000484/can-you-tweak-these-codes-for-userform-make-it-small-and-efficient