+ Reply to Thread
Results 1 to 15 of 15

Call UserForm from a variable number of ActiveX command buttons

  1. #1
    Registered User
    Join Date
    08-06-2013
    Location
    Hawaii, US
    MS-Off Ver
    Excel 2013
    Posts
    13

    Call UserForm from a variable number of ActiveX command buttons

    I want to be able to call one user form from multiple ActiveX command buttons. The problem is, the number of command buttons depends on user input on another worksheet, so it's variable.

    I've renamed all of the command buttons so they are named "CommandButton" & i, where i is an integer between 1 and, say, 200. I want each of these buttons to direct to the same UserForm where additional information can be entered.

    I can't think of a way around the event-handler procedure name.

    Sub [Command Button Name]_Click

    to call the User Form. I won't know the command button names, because I won't know how many there are (max i) until the user inputs.

    Basically, I want to create a For loop through the max i and have the event-handlers call the user form

    Any ideas?

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

    Re: Call UserForm from a variable number of ActiveX command buttons

    Code to add buttons and then assign dynamic event handler

    Please Login or Register  to view this content.
    Code for Class module CButtonEvt
    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    08-06-2013
    Location
    Hawaii, US
    MS-Off Ver
    Excel 2013
    Posts
    13

    Re: Call UserForm from a variable number of ActiveX command buttons

    Andy,

    Thank you so much for the attempt, but unfortunately this is not what I'm looking for.

    I've already created the variable number of buttons, which depends on the number of entries on another worksheet (the code is just embedded with various other formatting and project-specific code, etc. so I'm not including it here) as OLEObjects, and I've renamed each of them "CommandButton" & i, where i is the number 1 to the max number of buttons. I'm really happy with how this works, where the buttons are placed, and it fits with everything else I've got going on, so it seems I don't need the first part of your answer. I also have other command buttons and OLEObjects on the same worksheet, which complicates things.

    I would REALLY like help creating an event handler that calls one userform (which I've already created) from each of the buttons that has been created. Basically, I need to call an event handler for a variable number of buttons. After the user inputs information on another worksheet, the variable number will be defined, but to me, right now, it's unknown.

    Typically, I would just write a procedure:

    Sub CommandButton_Click
    ...

    or, more specifically in this case:

    Sub CommandButton1_Click
    ...

    Sub CommandButton2_Click
    ...

    and so on

    but since I don't know how many buttons there will be, I don't know the names of each of the buttons (there could be CommandButton20, or CommandButton200).

    Am I missing something from your answer? Any other advice?

    Thanks so much,
    Karen

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Call UserForm from a variable number of ActiveX command buttons

    Just to be clear. These "added CommandButtons", are they on the user form or are they the ActiveX Command Buttons on the worksheet(s).

    If all the buttons do is invoke the user form, I would go with Forms Buttons, that can be assigned to the same macro, which invokes the user form.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    08-06-2013
    Location
    Hawaii, US
    MS-Off Ver
    Excel 2013
    Posts
    13

    Re: Call UserForm from a variable number of ActiveX command buttons

    They're on a worksheet.

    The buttons will only invoke a userform, but as I understand it, you can't create Forms Buttons via VBA code. If there's a way to create Forms Buttons via VBA, I'd be happy to - but as it stands now, I've created OLEObject command buttons (which are ActiveX, yeah?).

    Thanks!

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

    Re: Call UserForm from a variable number of ActiveX command buttons

    I guess you are missing something.

    What happens when you press one of the newly added buttons in my example?
    It should display a message box with the name and caption of the button. If not then I can understand your confusion.

    Simply replace the msgbox code with a call to display the userform. If there are more than 1 userform to display then add code/property to class to determine which userform to show when clicked.

    You can adapt the code to loop through oleobjects and assign event handler rather than create them.

    Yes you can add forms controls to worksheet
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Call UserForm from a variable number of ActiveX command buttons

    Hi GreggProKH,

    Please check whether the below URL helps you:

    http://www.mrexcel.com/forum/excel-q...22-button.html


    Regards,
    Paresh J
    Click on "* Add Reputation" as a way to say thanks

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Call UserForm from a variable number of ActiveX command buttons

    Quote Originally Posted by GreggProKH View Post
    ...If there's a way to create Forms Buttons via VBA, I'd be happy to ...
    Please Login or Register  to view this content.
    Last edited by mikerickson; 07-03-2014 at 12:53 PM.

  9. #9
    Registered User
    Join Date
    08-06-2013
    Location
    Hawaii, US
    MS-Off Ver
    Excel 2013
    Posts
    13

    Re: Call UserForm from a variable number of ActiveX command buttons

    Thanks Andy and mikerickson.

    I changed the buttons to forms buttons (The commented out portion is what I had previously to create the ActiveX buttons):

    Please Login or Register  to view this content.
    and then a Macro ShowSGUserForm:

    Please Login or Register  to view this content.
    which should activate the SGMeasuresForm UserForm I have. However, I get "Run Time Error 438 - Object Doesn't Support this Property or Method". The only thing I can think of is that the issue is that I'm calling (indirectly) a userform from a form button, rather than an ActiveX button. Any truth to this? I feel like it shouldn't matter because I'm doing it indirectly and calling the macro from the button and the userform from the macro.

    Also, I used the same parameters (left,top,height,width) for my Form button that I did for my OLEObjects buttons and the formatting is all screwed up now. My parameters worked well in the OLEObjects.Add.

    Lastly, Andy - I'm still trying to integrate your original code into mine to see if I can get it to fit my needs. I'll let you know if I make progress and go that route instead of the forms buttons.

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Call UserForm from a variable number of ActiveX command buttons

    Is SGMeasuresForm in a normal code module and not a sheet's code module?

  11. #11
    Registered User
    Join Date
    08-06-2013
    Location
    Hawaii, US
    MS-Off Ver
    Excel 2013
    Posts
    13

    Re: Call UserForm from a variable number of ActiveX command buttons

    mikeerickson,

    Sorry for the late reply - just getting back to looking at it. Yes, it's in a normal code module (module named "ShowSGUserForm" - procedure called SGUserForm, as shown above), not the sheet's code module. Any ideas?

  12. #12
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Call UserForm from a variable number of ActiveX command buttons

    Trivial things like misspelling (is there an s in SGMeasuresForm)

  13. #13
    Registered User
    Join Date
    08-06-2013
    Location
    Hawaii, US
    MS-Off Ver
    Excel 2013
    Posts
    13

    Re: Call UserForm from a variable number of ActiveX command buttons

    Alright, I feel like I'm really close. I'm back to using the Class Module idea -- thanks, Andy.

    My class module, named BtnClass:
    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    I'm having two problems. The most important one:

    The buttons don't do anything because I get errors with both of these attempts:

    Please Login or Register  to view this content.
    The second problem is that I'd like to skip over my Clear Button (an OLEObject), but something's wrong with my If statement:
    Please Login or Register  to view this content.
    Any help!? Thanks so much!

  14. #14
    Registered User
    Join Date
    08-06-2013
    Location
    Hawaii, US
    MS-Off Ver
    Excel 2013
    Posts
    13

    Re: Call UserForm from a variable number of ActiveX command buttons

    What I've tried was adapted from John Walkenbach's example, p.529 in "Excel 2013 Power Programming with VBA" (also found here: http://www.j-walk.com/ss/excel/tips/tip44.htm and what you suggested, Andy.

    The difference (as you know) is that my buttons are on a worksheet, rather than a UserForm.

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

    Re: Call UserForm from a variable number of ActiveX command buttons

    The example uses form buttons for the Create/Remove events.

    The Clear button is named ClearButton
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Userform: Can you capture the selections from command buttons and call macros on close
    By darrenkaye in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2013, 06:34 AM
  2. Master Command Button to call on other Command Buttons located in other Workbooks
    By MrNickRegan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2013, 07:57 PM
  3. [SOLVED] Call command buttons by user-defined names
    By Willardio in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-14-2013, 10:58 AM
  4. ActiveX Command Buttons - Dont stick in the same place?
    By mworonuk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-08-2010, 02:44 AM
  5. [SOLVED] adding userform command buttons
    By borg in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-02-2006, 12:15 PM

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