+ Reply to Thread
Results 1 to 9 of 9

Can Multiple Buttons Invoke Same Event Handler?

  1. #1
    Registered User
    Join Date
    03-05-2010
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    5

    Can Multiple Buttons Invoke Same Event Handler?

    I have 10 command buttons which all do very much the same thing, just for different cells. The interface requires that I have the 10 separate buttons. Instead of having 10 separate CommandButton_Click procedures is it possible to have all of the buttons call the same event handler in my code when any of them are clicked?

    Thanks

    Joe

  2. #2
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848

    Re: Can Multiple Buttons Invoke Same Event Handler?

    You'll still have to have the 10 separate Click events, but you can have all calling the same event.

    Something like the following
    In the sheet module:
    Please Login or Register  to view this content.
    In a standard module:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Can Multiple Buttons Invoke Same Event Handler?

    If you have buttons on the worksheet then use Form Controls rather than ActiveX ones. Then you can assign the same macro to all buttons. Inside the macro use Application.Caller to determine which button was clicked.

    If you have a userform or are using ActiveX controls then you can create a class with events. Create a new object for each controls and store in a collection. All buttons will trigger the same event code.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Registered User
    Join Date
    03-05-2010
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Can Multiple Buttons Invoke Same Event Handler?

    Thanks Andy. I am using a Form CommandButton. However, I can't figure out how to assign it to an event handler other than the default <CommandButton.Name>_Click. I am sure it is pretty simple. Can you tell me how to do it? Do I assign all of the buttons the same name? Thanks

    Joe

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Can Multiple Buttons Invoke Same Event Handler?

    Buttons on a userform, yes?

    If so see John's example
    http://spreadsheetpage.com/index.php...er_dialog_box/

  6. #6
    Registered User
    Join Date
    03-05-2010
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Can Multiple Buttons Invoke Same Event Handler?

    No, the buttons are on a worksheet

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Can Multiple Buttons Invoke Same Event Handler?

    Then select all the buttons and use the Assign Macro option on the right click menu.

  8. #8
    Registered User
    Join Date
    03-05-2010
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Can Multiple Buttons Invoke Same Event Handler?

    Thanks Andy. This looks like it will work. Any idea how I can assign my own name to each button? The name field on the upper left of the sheet is not accepting any changes. As soon as I change it it changes back to the default name (e.g., Button16). Thanks

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Can Multiple Buttons Invoke Same Event Handler?

    Are you press enter within the name box to commit the name?

    Is the new name unique? If it is one that is already in use then it will revert to original.

+ 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