?
Solved

Excel VBA - UserForm control click

Posted on 2016-11-13
12
Medium Priority
?
95 Views
Last Modified: 2016-11-17
I have 14 CheckBoxes on a UserForm. 'chk1AM', chk1PM .... to ckh7AM, chk7PM.

I want to avoid having 14 _Click events. Would it be possible to have an 'event' that will be triggered if any of these are clicked? (So then I determine which one was clicked as per the other question I asked here earlier)

Thanks
0
Comment
Question by:hindersaliva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 41885537
That's not possible with VBA, but why not put all those values in one control like a combobox and have them pick from there? Can you attach your workbook?
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 41885538
Other than that I think the only thing you can do is to create a Sub that does what you want, and add one line of code to each checkbox that calls that sub passing the name of the control.
0
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 41885580
Hi,

It is possible with Class Module
in a class module named clsCheckBoxEvent use this code
Option Explicit
 
Public WithEvents CheckGroup As MSForms.CheckBox

Private Sub CheckGroup_Click()
 
MsgBox CheckGroup.Caption
 
End Sub

Open in new window

then in the userform module
Dim CheckBoxesColl As Collection

Private Sub UserForm_Initialize()
Dim CheckBoxHandler As ClsCheckBoxEvent

Set CheckBoxesColl = New Collection

For Each ctitem In Me.Controls
    If ctitem.DisplayStyle = fmDisplayStyleCheckBox Then
        Set CheckBoxHandler = New ClsCheckBoxEvent
        Set CheckBoxHandler.CheckGroup = ctitem
        CheckBoxesColl.Add CheckBoxHandler
    End If
Next
End Sub

Open in new window

Regards
EE20161113.xlsm
1
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:hindersaliva
ID: 41886112
Rgonzo, on my workbook I get this error. (however, your example works)
Error message
This is what's causing it, but I don't see why. I'm using Excel 2016, and I can see there is a reference to Microsoft Forms 2.0 Object Library.
Code where the error occurs
What could it be? Thanks
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41886116
Could you send a dummy?
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41886119
then try
Dim CheckBoxesColl As Collection
Private Sub UserForm_Initialize()
Dim CheckBoxHandler As ClsCheckBoxEvent

Set CheckBoxesColl = New Collection

For Each ctitem In Me.Controls
    If TypeName(ctitem) = "CheckBox" Then
        Set CheckBoxHandler = New ClsCheckBoxEvent
        Set CheckBoxHandler.CheckGroup = ctitem
        CheckBoxesColl.Add CheckBoxHandler
    End If
Next
End Sub

Open in new window

0
 

Author Comment

by:hindersaliva
ID: 41886122
Rgonzo, now I don't get the error but the Checkbox click doesn't produce the message box.
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41886125
a dummy would be helpful
0
 

Author Comment

by:hindersaliva
ID: 41886136
See attached. Ignore the sheet, go to UserForm.
EE-troubleshoot-controls-click-clas.xlsm
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41886140
see remark
'Dim CheckBoxesColl As Collection 'should be and is already defined as global
    Dim CheckBoxHandler As clsCheckboxEvent
    
    Set CheckBoxesColl = New Collection
    
    For Each ctitem In Me.Controls
        If TypeName(ctitem) = "CheckBox" Then
            Set CheckBoxHandler = New clsCheckboxEvent
            Set CheckBoxHandler.CheckGroup = ctitem
            CheckBoxesColl.Add CheckBoxHandler
        End If
    Next

Open in new window

0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41886143
your captions are all the same then use name

Option Explicit
 
Public WithEvents CheckGroup As MSForms.CheckBox

Private Sub CheckGroup_Click()
 
    MsgBox CheckGroup.Name
 
End Sub

Open in new window

0
 

Author Closing Comment

by:hindersaliva
ID: 41892374
Thanks Rgonzo. That worked beautifully!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question