+ Reply to Thread
Results 1 to 4 of 4

Code to rationalise multiple command buttons in VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Code to rationalise multiple command buttons in VBA

    Hi there

    Would anybody be able to advise me on my problem?

    I have 63 command buttons on a userform in multipage controls. Each command button, when clicked, does almost the same thing. For example:

    Private Sub cmd2_1_1_Click()
        
        If cmd2_1_1.BackColor = 13485434 Then
        Exit Sub
        End If
        
        cmd2_1_1.BackColor = 13485434
        
        CCaption = cmd2_1_1.Caption: ECaption = "Loft Quilt ": Supplier = "Central Purchasing": GLCode = "5002"
        
        Call ButtonClick
        
    End Sub
    Private Sub cmd2_1_2_Click()
        
        If cmd2_1_2.BackColor = 13485434 Then
        Exit Sub
        End If
            
        cmd2_1_2.BackColor = 13485434
           
        CCaption = cmd2_1_2.Caption: ECaption = "Loft Quilt ": Supplier = "Central Purchasing": GLCode = "5002"
        
        Call ButtonClick
        
    End Sub
    and so on. Is there a way to create a sub which will recognise the name of the command button, eg 2_1_1 for cmd 2_1_1 and 2_1_2 for cmd2_1_2 so that I can call that sub to execute the code? There is a different caption name for each button.

    Thanks all.

  2. #2
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Code to rationalise multiple command buttons in VBA

    Any thoughts? I wonder if an event handler is the answer. Help!

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code to rationalise multiple command buttons in VBA

    I'd create an Event Handler because I couldn't be bothered adding code to each command button, but if you don't feel comfortable with classes, you might get away with something like:

    Private Sub CommandButton1_Click()
        BtnClick Me.CommandButton1
    End Sub
    Sub BtnClick(ByRef btn As MSForms.CommandButton)
    
        If btn.BackColor = 13485434 Then Exit Sub
        btn.BackColor = 13485434
           
        CCaption = btn.Caption
        ECaption = "Loft Quilt "
        Supplier = "Central Purchasing"
        GLCode = "5002"
        
        ButtonClick
        
    End Sub
    
    Sub ButtonClick()
        MsgBox "sdfsdf"
    End Sub
    If you're interested, the event handler class would look something like:

    Class CbtnHandler:
    Public WithEvents btn As MSForms.CommandButton
    
    Private Sub btn_Click()
    
        If btn.BackColor = 13485434 Then Exit Sub
        btn.BackColor = 13485434
        ButtonClick
    
    End Sub
    
    Private Sub ButtonClick()
        
        MsgBox btn.Caption
    
    End Sub
    Form implementation:
    Dim btns As Collection
    
    
    Private Sub UserForm_Initialize()
    Dim oBtn As CbtnHandler
    Dim ctl As Object
    
    Set btns = New Collection
    
    For Each ctl In Me.Controls
        If TypeName(ctl) = "CommandButton" Then
            Set oBtn = New CbtnHandler
            Set oBtn.btn = ctl
            btns.Add oBtn
        End If
    Next ctl
    
    
    End Sub
    This approach would need no code in the button click subs
    Last edited by Kyle123; 11-16-2012 at 07:24 AM.

  4. #4
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Code to rationalise multiple command buttons in VBA

    Wow! When am I going to get to this level of understanding! (probably never...)

    Kyle, thanks for these options. I will try them out.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1