Consulting

Results 1 to 3 of 3

Thread: Class module for userform command buttons

  1. #1

    Class module for userform command buttons

    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?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    This seems to work. Thank-you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •