+ Reply to Thread
Results 1 to 7 of 7

Using VBA to assign a macro to a button

  1. #1
    Registered User
    Join Date
    02-02-2010
    Location
    playa del carmen, mexico
    MS-Off Ver
    ms office 2007
    Posts
    52

    Using VBA to assign a macro to a button

    I am currently designing a user form which is populated upon activation with as many buttons as there are items in a variable list.
    I am resizing, repositioning, and renaming each button depending on how many items are in the list.
    What I need to be able to do and haven't been able to find so far, is how to assign a macro to the button once it has been generated.
    I should only need one macro, regardless of how many buttons there are, and the macro itself will just take the button's caption value to then decide what to do next.

    Thanks for your help

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Using VBA to assign a macro to a button

    Why do you need a button for each item?

    You don't assign a macro to a userform button. You could write a Procedure in the form's module & call that with the buttons
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using VBA to assign a macro to a button

    You need a class with a button variable declared WithEvents and a Click event handler. Then when you add buttons to the form, you create a new instance of your class, assign the button you created to its variable, and then add the class instance to an array or a Collection.
    If you need more details, please post back or search the forum for WithEvents - I'm sure there are several examples.
    Remember what the dormouse said
    Feed your head

  4. #4
    Registered User
    Join Date
    02-02-2010
    Location
    playa del carmen, mexico
    MS-Off Ver
    ms office 2007
    Posts
    52

    Re: Using VBA to assign a macro to a button

    OK, I think I could probably explain this better, because I think the withevents solution may work, but might even be a little overkill.
    I am going to have a dynamic list of names, and for each name, each name will have an entire sheet with the actual name.
    I would like to call a form, which would populate itself with as many buttons as names, and then each button would activate the appropriate sheet.
    Now I know this would be easier as a listbox, or even straight hyperlinks right now, but partly for aesthetics, and also because I am pretty sure I will have other uses for the same form later in the programming, and would like to have a flexibility to its usage if possible, I would like to make it with a form full of buttons.
    I have completed the design stage of it and coded the population and reshaping side of things, I would like to know how to get VBA to recognise which button is being clicked, and take a value from the form upon the button being clicked, which can be used in other parts of my code.

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Using VBA to assign a macro to a button

    I think WithEvents is the way to go for that. It's not that complicated and definitely not overkill compared to the alternatives.

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Using VBA to assign a macro to a button

    Way over complicated. Populate a ComboBox or ListBox with the names, then use the selection to activate the sheet

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Using VBA to assign a macro to a button

    Use the click-event
    Please Login or Register  to view this content.



+ 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