I am trying to create a dynamic number of labels and textboxes with the following code and it sort of works.
However, I want to use a variable to make sure that the labels and the textboxes don't go outside of the userform.
I have tried a select case and if else statements but they offset the labels/textboxes in the wrong way.
Ideally, I would like to have 3 or 4 columns with pairs of labels/textboxes. With 25 to 35 pairs in each column.
Code for the labels:
Code for the textboxes:
I am glad for all suggestions!
However, I want to use a variable to make sure that the labels and the textboxes don't go outside of the userform.
I have tried a select case and if else statements but they offset the labels/textboxes in the wrong way.
Ideally, I would like to have 3 or 4 columns with pairs of labels/textboxes. With 25 to 35 pairs in each column.
Code for the labels:
Code:
Private Sub CommandButton2_Click()
Dim i As Long
Dim number As Integer
Dim productID As Variant
Dim lastrow
Dim j As Integer
Dim labelcounter As Integer
Dim myvalue As Variant
Dim thelabel As Variant
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
lastrow = ws.Cells(Rows.Count, 1).End(xlUp).row
number = lastrow
Dim txtbox As Control
For i = 1 To lastrow
Set txtbox = UserForm3.Controls.Add("Forms.Label.1")
productID = Sheet1.Range("A" & i + 2).Value
With txtbox
.Name = "textbox" & i
.Height = 18
.Width = 100
.Left = 10
.Caption = productID
.Top = 20 * i * 1
End With
Next i
End Sub
Code for the textboxes:
Code:
Private Sub CommandButton3_Click()
Dim i As Long
Dim number As Integer
Dim productID As Variant
Dim lastrow
Dim j As Integer
Dim labelcounter As Integer
Dim myvalue As Variant
Dim thelabel As Variant
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
lastrow = ws.Cells(Rows.Count, 1).End(xlUp).row
number = lastrow
Dim txtbox As Control
For i = 1 To lastrow
Set txtbox = UserForm3.Controls.Add("Forms.TextBox.1")
productID = Sheet1.Range("D" & i + 2).Value
With txtbox
.Name = "textbox" & i
.Height = 18
.Width = 100
.Left = 125
.Value = productID
.Top = 20 * i * 1
.TabIndex = i
End With
Next i
End Sub
I am glad for all suggestions!