Consulting

Results 1 to 5 of 5

Thread: Call a Macro with 40 different CommandButtons

  1. #1

    Call a Macro with 40 different CommandButtons

    Hi,

    I have a UserForm with 40 CommandButtons and I have the following macro to call another UserForm with that CommandButtons.

    Sub Show_checkin()
        Load checkin
        checkin.Show
    End Sub
    if I add the following code to each button, everything works fine

    Private Sub button1_Click()
        Call Show_checkin
    End Sub
    Private Sub button2_Click()
        Call Show_checkin
    End Sub
    Private Sub button3_Click()
        Call Show_checkin
    End Sub
    Private Sub button4_Click()
        Call Show_checkin
    End Sub
    Private Sub button5_Click()
        Call Show_checkin
    End Sub
    '
    'and this goes up to 40th button...
    What I want to know is; is there any shorter way to do this action? I mean one code will work for every button.

    Thanks in advance

  2. #2
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    Hi TG,

    What you need is a Class Module. JW explains how here :
    http://www.j-walk.com/ss/excel/tips/tip44.htm

    HTH

  3. #3

  4. #4
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    Why do you have 40 commandbuttons performing the same function ?
    Only ask as it maybe easier if you just designed the need for 40 out rather then look for an easier method of call the same procedure?
    Kind Regards,
    Ivan F Moala From the City of Sails

  5. #5
    VBAX Contributor Aaron Blood's Avatar
    Joined
    Sep 2004
    Location
    Palm Beach, Florida, USA
    Posts
    130
    Location
    Quote Originally Posted by Ivan F Moala
    Why do you have 40 commandbuttons performing the same function ?
    Only ask as it maybe easier if you just designed the need for 40 out rather then look for an easier method of call the same procedure?
    Perhaps the macro performs a different op depending on the button caller?
    Only reason I could think of... But even so, I'd probably opt for a listbox then.

Posting Permissions

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