Creating controls on userform on the fly with VBA

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
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:
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!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi Akuini,

thank you for your reply! Your answer helped me!
 
Upvote 0
Code:
.Value = Sheet1.Cells(i, 4).Value

How can I get a value from a listbox into the value of the textbox or the caption of the label?
 
Upvote 0
For a single column Listbox:

Code:
With ListBox1
For i = 0 To .ListCount - 1
Me.Controls("txtBox" & i + 1).Value = .List(i)
Next
End With

for a multi column Listbox (for first column):
Code:
Me.Controls("txtBox" & i + 1).Value = .List(i, 0)

Note: in Listbox, row & column starts at 0
 
Upvote 0
Hi Akuini,

thank you for your reply! I got it to work with your code!
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top