1

I have a userform and on this userform I have a checkbox CheckNoncl1. Here's the test code I made for click event of this checkbox and it works fine:

Private Sub CheckNoncl1_Click()
    If CheckNoncl1.Value = True Then
        MsgBox "true"
        Else
        MsgBox "false"
    End If
End Sub

But what if I want to make a dynamic reference and use Me I get the "Method or data member not found". I suspect it is because "Me" in this case refers to the userform, not the checkbox.:

Private Sub CheckNoncl1_Click()
    If Me.Value = True Then 'Error!
        MsgBox "true"
        Else
        MsgBox "false"
    End If
End Sub

How do I avoid this problem? Is there something I get completely wrong about event handlers for checkboxes?

Igor Cheglakov
  • 525
  • 5
  • 11
  • 5
    `Me` will always refer to the "parent" UserForm, not to an individual control on the form. In technical terms: The UserForm is a class. `Me` can be used as a shortcut to refer to the class in code that's part of that class. There is no shortcut for the control name. But you can create one by declaring an object and "setting" it to the control. Not sure why you'd want that, unless it's because you want to type less... – Cindy Meister Dec 09 '19 at 17:58
  • I need several checkboxes on my userform doing basically the same thing (enabling/disabling corresponding combo-box) and I wondered if I can only write them one by one, by hand. – Igor Cheglakov Dec 09 '19 at 18:03
  • You could make a subroutine that does what you want with all the checkboxes, and pass the control object to that routine as an argument... that's about as close as you are going to get. – braX Dec 09 '19 at 18:12
  • @braX Excuse me, what is 'control object' exactly? – Igor Cheglakov Dec 09 '19 at 18:14
  • `Sub ProcessCheckbox(chk as Control)` and then to call it... `ProcessCheckbox CheckNoncl1` – braX Dec 09 '19 at 18:17
  • 2
    Possible duplicate of [VBA Many buttons point to the same _Click sub](https://stackoverflow.com/q/4860724/11683) – GSerg Dec 09 '19 at 18:17
  • @Cindy Meister Kinda. I guess I'm gonna code it by hand. – Igor Cheglakov Dec 09 '19 at 18:41

0 Answers0