0

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?

2
  • 1
    If this code is in a regular module, how do you call it? You will need a reference to the UF (maybe pass it as parameter) Commented Jul 10 at 11:24
  • 1
    When you create your checkboxes, store references to each of them in an array or dictionary that is accessible by both the creating function and other modules. When your toggle control is clicked, you access the checkbox control object array/dictionary and filter the controls to change the ones you want. Commented Jul 10 at 14:11

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Following on from your answer, I realised, I didn't mention: The togglebutton is dynamically created as well - therefore the Module. Reference would be cbName, so all good now that I have frm reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.