PDA

View Full Version : Class module for userform command buttons



Indigenous
07-16-2018, 08:14 AM
I was searching online for coding command buttons as a group and came across this example.

http://www.vbaexpress.com/kb/getarticle.php?kb_id=85

I was wondering if it's possible to make the code process more than 1 bunch of Command Buttons in the same form.

Basically I have three sets/groups of buttons with the following names.
- CommBtnMor_i, where is between 1 and 7
- CommBtnNJ_A, CommBtnNJ_B, CommBtnNJ_C, etc
- CommBtnCal_k, where k can be any string

Each group has different types of coding obviously.

Is it possible or not?

Paul_Hossler
07-16-2018, 12:56 PM
Not exactly sure, but this might be a start


In clsButton



Option Explicit

Public WithEvents CommandButtonGroup As CommandButton

Private Sub CommandButtonGroup_Click()
If (CommandButtonGroup.Name Like "CommBtnMor_#") Then
MsgBox "1 - You pressed " & CommandButtonGroup.Caption

ElseIf (CommandButtonGroup.Name Like "CommBtnNJ_?") Then
MsgBox "2 - You pressed " & CommandButtonGroup.Caption

ElseIf (CommandButtonGroup.Name Like "CommBtnCal_*") Then
MsgBox "3 - You pressed " & CommandButtonGroup.Caption

End If

End Sub




In Userform


Option Explicit

Dim Buttons() As New clsButton

Private Sub UserForm_Initialize()
Dim Ctrl As Control
Dim Count As Long
For Each Ctrl In UserForm1.Controls
If TypeName(Ctrl) = "CommandButton" Then
Ctrl.Caption = Ctrl.Name
If (Ctrl.Name Like "CommBtnMor_#") Or (Ctrl.Name Like "CommBtnNJ_?") Or (Ctrl.Name Like "CommBtnCal_*") Then
Count = Count + 1
ReDim Preserve Buttons(1 To Count)
Set Buttons(Count).CommandButtonGroup = Ctrl
End If
End If
Next
End Sub

Indigenous
07-16-2018, 08:02 PM
This seems to work. Thank-you.