Dynamically adding Userform controls

Damo10

Active Member
Joined
Dec 13, 2010
Messages
460
Hi,

I am trying to add the required number of controls to my userform based on how many items are in sheet 2, this bit I have working but what I cant work out is that when the user clicks on the checkbox I would like the label caption of the the label lblUser to display the Environ username.

Can somebody tell me if this is possible?

Code:
Private Sub UserForm_Initialize()


Dim Rng As Range
Dim c As Range
Dim Ctrl As Object
Dim x As Long
Dim Count As Long


Set Rng = Sheet2.Range(Sheet2.Range("C5"), Sheet2.Range("C" & Rows.Count).End(xlUp))
x = 50
Count = 1


For Each c In Rng


If Not c = Empty Then
    
Set Ctrl = Me.Controls.Add("Forms.Label.1", "lblItem" & Count, True)


With Ctrl
    .Width = 150
    .Height = 30
    .FontSize = 16
    .Top = x
    .Left = 30
    .Caption = c
End With


Set Ctrl = Me.Controls.Add("Forms.Checkbox.1", "chk" & Count, True)


With Ctrl
    .Top = x
    .Left = 190
End With


Set Ctrl = Me.Controls.Add("Forms.Label.1", "lblUser" & Count, True)


With Ctrl
    .Width = 150
    .Height = 30
    .FontSize = 16
    .Top = x
    .Left = 210
End With


x = x + 40
Count = Count + 1


End If


Next c


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You have multiple labels which have a name starting with lblUser, which one do you want to set the caption of when a checkbox is clicked?
 
Upvote 0
Hi,

Each of the labels has a number on the end as do the checkboxes so corresponding lbluser to the checkbox would be the one to set the caption

Example: if chk2 was clicked then lblUser2 has caption set

Regards,
 
Last edited:
Upvote 0
Insert a class: Class1 and put this code:

Code:
Public WithEvents tbxCustom1 As MSForms.CheckBox
'
Private Sub tbxCustom1_Change()
    'MsgBox "clicks on the checkbox: " & tbxCustom1.Name
    Dim num As Long
    num = Mid(tbxCustom1.Name, 4)
    If tbxCustom1.Value = True Then
        UserForm1.Controls("lblUser" & num).Caption = Environ("username")
    Else
        UserForm1.Controls("lblUser" & num).Caption = ""
    End If
End Sub

Update your code:

Code:
[COLOR=#0000ff]Dim colTbxs As Collection 'Collection Of Custom checkbox[/COLOR]


Private Sub UserForm_Initialize()
    Dim Rng As Range
    Dim c As Range
    Dim Ctrl As Object
    Dim x As Long
    Dim Count As Long
[COLOR=#0000ff]    Dim clsObject As Class1[/COLOR]
[COLOR=#0000ff]    Set colTbxs = New Collection[/COLOR]


    Set Rng = Sheet2.Range(Sheet2.Range("C5"), Sheet2.Range("C" & Rows.Count).End(xlUp))
    x = 50
    Count = 1
    For Each c In Rng
        If Not c = Empty Then
            Set Ctrl = Me.Controls.Add("Forms.Label.1", "lblItem" & Count, True)
            With Ctrl
                .Width = 150
                .Height = 30
                .FontSize = 16
                .Top = x
                .Left = 30
                .Caption = c
            End With
            Set Ctrl = Me.Controls.Add("Forms.Checkbox.1", "chk" & Count, True)
            With Ctrl
                .Top = x
                .Left = 190
            End With
            Set Ctrl = Me.Controls.Add("Forms.Label.1", "lblUser" & Count, True)
            With Ctrl
                .Width = 150
                .Height = 30
                .FontSize = 16
                .Top = x
                .Left = 210
            End With
            x = x + 40
            
[COLOR=#0000ff]            Set clsObject = New Class1[/COLOR]
[COLOR=#0000ff]            Set clsObject.tbxCustom1 = Me.Controls("chk" & Count)[/COLOR]
[COLOR=#0000ff]            colTbxs.Add clsObject[/COLOR]
            
            Count = Count + 1
        End If
    Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,917
Members
449,055
Latest member
KB13

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