Listbox Created In Runtime - _Click event not triggering

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm creating a listbox at runtime for my user form ...

Code:
public cCont as Control

Code:
Private Sub UserForm_Initialize()
   
    Dim temp_ws As Worksheet
    Set temp_ws = wb_sched.Worksheets("temp_ws")
    Set cCont = Me.Controls.Add _
            ("Forms.Listbox.1", "lb_mrexcel")
    ...

    With Me
        With cCont
            .Left = 14
            .Top = 417
            .Height = 76.55
            .Width = 215.25
            .Locked = False
            .ForeColor = RGB(0, 52, 89)
            .ColumnCount = 4
            .ColumnWidths = "30;100;80;95"
            .List = Range("missing_all").Value
            .ListStyle = fmListStylePlain
            .MultiSelect = fmMultiSelectSingle
        End If
    End With
    ...
End Sub

However, when I click on this newly created listbox, the control_click does not trigger.

Code:
Private Sub cContl_Click()
    
    If Not mbEvents Then Exit Sub
    mbEvents = False
    Debug.Print Me.Name, "lb_mrexcel_Click() called"
    
    With cCont
        Debug.Print Me.Name, "lb_mrexcel_Click() ListIndex: " & .ListIndex & " (" & .List(.ListIndex) & ")"
        agf = 1 'referred to from uf2_assess_sched
        group_1.Show
    End With
    mbEvents = True
End Sub

Anyone have any idea where I'm going wrong?
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Code:
Private Sub cContl_Click()
should be
Code:
Private Sub cCont_Click()

... but that still made no difference.
 
Upvote 0
Change the first code line :
Code:
[COLOR=#574123]public cCont as Control[/COLOR]

To:

Code:
Private WithEvents cCont  As MSForms.ListBox
 
Upvote 0
Hi Jaafar, thank you for your suggestion.

I put your suggested code in the userform initialize routine of my userform. I'm getting an error: "Invalid attribute in Sub or Function".
My original public declaration for cCont was in a module executed when my workbook opened ... basically a routine that defines and sets all my public variables. When I tried to substitute the original code with yours there, I was told I could only use it in object code, which is why I put it in my initialize code.
 
Upvote 0
It needs to be at the top of the userform module, not inside any routine.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,127
Members
452,381
Latest member
Nova88

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