vbacheckboxdynamictogglebutton

VBA: Check/Tick specific dynamic checkbox when Togglebutton is clicked


I have created a UF reading in various tabs (opt) with each tab having between 10 to 19 entries which I display in a Multipage checkbox list. Now I'm trying to create various Togglebuttons which uncheck certain checkboxes. I.e. Togglebutton "outside" should uncheck all the inside activities which have been selected from the list. (Like a quick selector).

I have created the checkboxes like this:

For opt = 3 To Sheets.Count 
    lastRow = Sheets(opt).Cells(Rows.Count, 4).End(xlUp).row
    'loop for all options
    For i = 1 To lastRow - 1
        Set cb = MultiPage1.Pages(opt - 3).Controls.Add("Forms.CheckBox.1", "CheckBox_" & opt & "_1_" & i)

So each CB has a specific name. I want to uncheck a certain CB (i.e. CheckBox_14_1_18 = False); however, since I wrote the code for the Togglebutton into a (separate) Module, I now struggle to call that specific CB by name/reference.

I've tried:

Dim CBname As String
lastRow = Sheets(Sheets.Count).Cells(Rows.Count, 4).End(xlUp).row
            CBname = "CheckBox_" & Sheets.Count & "_1_" & lastRow
' CBname.Value = False --> invalid qualifier
' Me.Controls(CBname).Value = False --> Method or data member not found

How would I call the specific checkbox in the module?


Solution

  • Following on from @FunThomas' suggestion...

    In your userform the handler for the toggle button might be (eg):

    Private Sub ToggleButton1_Click()
        HideShowIndoorOptions Me '<< pass a reference to the form instance to the called method
    End Sub
    

    In your regular module (simplified example):

    Sub HideShowIndoorOptions(frm As UserForm1)
        Dim cbName As String
        cbName = "CheckBox_14_1_18"
        frm.Controls().Value = False  '<< use the reference supplied by the calling code
    End Sub