+ Reply to Thread
Results 1 to 27 of 27

Add Command Buttons to User Form at Runtime

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Add Command Buttons to User Form at Runtime

    I have a number of macros that I would like to call from a User Form. I want to be able to pass an array of the names of these macros to a function that will runtime create a UserForm with Command Buttons linked to these macros. Is anybody able to assist with this?

    Attached is as far as I have got.

    I am having difficulty with the following:
    1. Create the form dynamically (currently using the first form in ThisWorkbook)
    2. Spacing the Cmd Buttons over the form (currently overwriting the same button)
    3. Adding the Calls to the Command Buttons
    Attached Files Attached Files
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Add Command Buttons to User Form at Runtime

    Here's a simple example of 1 and 2.
    Please Login or Register  to view this content.
    I'll also post this as it's quite handy when playing about with this sort of thing.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    Thanks Norie. +1

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    See attached workbook. Almost there.

    Two problems left.

    I haven't thought through how to initialize the form each time I send an array of macros to it. I have thought of two options:
    1. I don't add the form dynamically. Instead I create a named form and programatically add the buttons to this. However the forms code would have to be erased every time before the buttons are added. Is this possible?!
    2. I add the form at runtime (like I originally planned) but somehow set the form to self destruct when the form is closed. Is this possible?!


    The second problem - just an aesthetic thing - I would prefer to see the buttons spread over the form rather than one long column. I'm not sure how to do this. (Maybe count number of macros to add and then add the buttons as, say, 3/4/5 columns by X no. of rows - depending on how many buttons we will have to play with).
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    Almost there Only one thing left:

    I would prefer to see the buttons spread over the form rather than appearing as one long column. I'm not sure how to do this. (Maybe count number of macros to add and then add the buttons as, say, 3/4/5 columns by X no. of rows - no. of columns chosen depending on how many buttons we will have to play with).
    Attached Files Attached Files

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Add Command Buttons to User Form at Runtime

    If you want to remove the temporary form after it's closed put this after the code to show the form.
    Please Login or Register  to view this content.
    Changing the layout of the buttons is straightforward, but the exact code really depends on how you want to show them.

    All you need to do is change Top and Left appropriately as you add the buttons.

    Please Login or Register  to view this content.

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    To Norie: Nice work. I like it!
    Last edited by mc84excel; 10-10-2013 at 10:27 PM.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    Well I have been making a lot of progress (thanks to Norie ). See attached.

    A few things I have planned:
    1. Have the buttons placed from left to right then down (instead of down then left to right)
    2. Work out a way to have a visually pleasing number of rows & columns. As can be seen from my sub, I am toying with the idea of using mod (once the number of buttons is greater than 10) to determine whether the number of buttons would fit neatly into X number of columns (say 5/7/9 columns). If no mod found on the desired column options then I will have to choose the number of columns that will have the closest fit (so that there isn't a large empty space on the last row). I'm not sure how to go about that yet.
    Attached Files Attached Files

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Add Command Buttons to User Form at Runtime

    1 You just need to swap things around a bit.

    2 What is a 'visually pleasing' no of rows and columns?

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    1. I've solved the 'left to right'. See attached.

    2. A good question. I guess I don't want to see a really elongated user form. (I would like to see the buttons laid out in a ratio that doesn't cause the form to hog the screen). I also don't want to see a single button on the last row. I am stumped over a good way to achieve these two objectives.

    For sake of sanity, I think a max of 50 buttons per form would be more then could ever possibly need.
    I would prefer to work in columns of 5/7/9. (Odd numbers to help reduce problems with Mod function)

    For less than 10 buttons, I have a Select Case choose a preset number of columns & rows (refer code "SetMaxRowsAndCols")

    But how do I work button quantities 11 to 50?
    I was thinking of a loop. For qty X, does it mod 0 on 5? If yes choose 5 cols. Else does it mod 0 on 7? If yes choose 7. And so on. Obviously not all numbers between 11 to 50 will divide neatly into 5, 7 or 9. So after running the 'perfect division' loop and getting nowhere, it would need to check the next closest match. One gap on the last row is acceptable. So for X + 1, does it mod 0 into 5/7/9? And so on.
    Attached Files Attached Files

  11. #11
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    I've made some changes to the code.
    • Allow different button name to macro name (array is made up of pairs, 1st=button name, 2nd=macro name)
    • Delete Form after close (Thanks Norie)
    • Unload Form before running sub
    • Button width set by len of largest button name string
    • Attempted to calc a more pleasing ratio of columns to rows (THIS STILL WIP )

    Updated Module below:

    Please Login or Register  to view this content.
    Last edited by mc84excel; 10-14-2013 at 09:48 PM.

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    Some array code is required for the updated module in the previous post (have to do separate post due to exceeded character limit!)


    Please Login or Register  to view this content.

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

    Re: Add Command Buttons to User Form at Runtime

    Out of interest, what's the use case for this? Wouldn't it just be easier to add controls dynamically in the normal way without adding them to the project?

  14. #14
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    Quote Originally Posted by Kyle123 View Post
    Out of interest, what's the use case for this?
    I need a userform to act as a temporary menu for the end user to choose an option from. The number of buttons I require will keep changing over time.

    The code is currently working but I have two headaches:
    1. Choosing an aesthetic layout of buttons & form height/width ratio. http://www.excelforum.com/excel-gene...d-outputs.html
    2. I can't use breakpoints in the sub called from the button. http://www.excelforum.com/excel-prog...t-runtime.html


    Quote Originally Posted by Kyle123 View Post
    Wouldn't it just be easier to add controls dynamically in the normal way without adding them to the project?
    I discovered yesterday that I had learnt Userforms wrong. I need to unlearn everything I thought I knew about UserForms and re-learn it correctly

    I'm confused "adding controls without adding them to the project"? Could you give a quick demo?
    Last edited by mc84excel; 11-21-2013 at 06:15 PM.

  15. #15
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Add Command Buttons to User Form at Runtime

    Why do you not use the Ribbon or contextmenus if you require a menu? If you must use a userform may you not use comboboxes or listboxes over so many buttons?

  16. #16
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    Quote Originally Posted by Izandol View Post
    Why do you not use the Ribbon or contextmenus if you require a menu? If you must use a userform may you not use comboboxes or listboxes over so many buttons?
    Thanks. Both are good suggestions. And normally I would do just that.

    The thing is I have this one-off project where I need to use a UserForm to display a grid of buttons.

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Add Command Buttons to User Form at Runtime

    mc84excel

    Is all this code you've posted just to allow the user to choose an option?

  18. #18
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    Quote Originally Posted by Norie View Post
    Is all this code you've posted just to allow the user to choose an option?
    Yes and no - There is more to it than just selecting an option. I can't say much more than that. Anyway I broke down the code into portions and only uploaded the part that I need solving.

    (But yes I am guilty of code bloat on this particular project if that's what you are inferring ).

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Add Command Buttons to User Form at Runtime

    Any chance you could give us a hint of what you are actually trying to do?

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

    Re: Add Command Buttons to User Form at Runtime

    I'm on my ipad, so I can't really give you an example for your specific question, so here are a couple of more general examples:

    An answer I wrote on SO: http://stackoverflow.com/a/10596866/1240154 - this is the most simple

    Possibly a little bit closer to what you are trying to do, but more complex - there's more functionality than you need here but it adds buttons dynamically to a grid of buttons: http://www.excelforum.com/excel-prog...eferences.html

    I'm sure I've given you an example of this before though when you wanted a custom drop down box where you could add rows

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

    Re: Add Command Buttons to User Form at Runtime

    I think that this is what you are trying to do:

    Events handling class - called cButtons - In this case we don't strictly need properties, we could have made the private declarations public and dispensed with them, but this is better practice
    Please Login or Register  to view this content.
    Blank userform for building the menu - called ufBaseForm
    Please Login or Register  to view this content.
    Module containing subs to run and code to kick things off
    Please Login or Register  to view this content.

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

    Re: Add Command Buttons to User Form at Runtime

    I reckon this is a better approach, it's probably a bit more logical

    Events handling class - called cButtons
    Please Login or Register  to view this content.
    Blank userform for building the menu - called ufBaseForm
    Please Login or Register  to view this content.
    Module containing subs to run and code to kick things off
    Please Login or Register  to view this content.

  23. #23
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Thumbs up Re: Add Command Buttons to User Form at Runtime

    Thanks a lot

    I've tested it out now and have a few questions/comments:
    1. I don't get the "Can't enter break mode" issue! For my learning - Why does your code not cause this?
    2. :S It uses the same button name as the sub name. But I should be able to rework the code to pick it up from every second element...
    3. The buttons appear as a column.
    Attached Files Attached Files
    Last edited by mc84excel; 11-28-2013 at 07:55 PM.

  24. #24
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    Quote Originally Posted by Kyle123 View Post
    Possibly a little bit closer to what you are trying to do, but more complex - there's more functionality than you need here but it adds buttons dynamically to a grid of buttons: http://www.excelforum.com/excel-prog...eferences.html
    You're right, this concept is closer to what I am working on. Interesting

    Quote Originally Posted by Kyle123 View Post
    I'm sure I've given you an example of this before though when you wanted a custom drop down box where you could add rows
    You did and I appreciate it. I found it very helpful. Unfortunately my class skills are none-existent. I will learn them someday (Just working my way up by learning custom types!)

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

    Re: Add Command Buttons to User Form at Runtime

    Quote Originally Posted by mc84excel View Post
    I don't get the "Can't enter break mode" issue! For my learning - Why does your code not cause this?
    I've never had this, so I can only speculate, you're trying to debug code whilst automating the IDE, trying to do two things at once sounds like a recipe for disaster so at a guess MS have stopped you doing it to prevent you doing any damage/save headaches.

    Since my code doesn't attempt to automate the IDE (and I can't actually think of an actual instance where I'd want to do so, except for maybe making an add-in to make development easier, something like MZTools), this scenario never occurs.
    Quote Originally Posted by mc84excel View Post
    It uses the same button name as the sub name
    Why do you care what the buttons are called? You reference them by location in the collection
    Quote Originally Posted by mc84excel View Post
    The buttons appear as a column
    Indeed, I'm not doing it all for you besides I can't even see what you're trying to do since I can't run your code as I've no intention of allowing trusted access to the VBA project.

    Quote Originally Posted by mc84excel View Post
    Unfortunately my class skills are none-existent. I will learn them someday
    Learn them instead of trying to automate the IDE, they're far more useful, less hacky and not really complicated. Automating the IDE is nasty you've done UDTs now, you've got no excuse...

  26. #26
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    Thanks for the info Kyle

    Quote Originally Posted by Kyle123 View Post
    Why do you care what the buttons are called?
    I have my reasons

    Quote Originally Posted by Kyle123 View Post
    Indeed, I'm not doing it all for you
    I wasn't expecting you to. Many thanks for what you provided.

    Quote Originally Posted by Kyle123 View Post
    you've done UDTs now, you've got no excuse...
    Almost done learning UDTs But you're right. I can't keep putting it off

  27. #27
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Add Command Buttons to User Form at Runtime

    SOLVED. I've tweaked Kyles code so that the form does what I want it to.
    Below is a generic version for anyone who needs this code.

    Form Code:
    Please Login or Register  to view this content.
    Module code:
    Please Login or Register  to view this content.
    Class code:
    Please Login or Register  to view this content.

    There are a few array functions used by the code above, see below:
    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)

Similar Threads

  1. [SOLVED] Option buttons on a user form
    By Aland2929 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2013, 07:39 AM
  2. [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
  3. command buttons with hyperlink to form
    By stats09 in forum Access Tables & Databases
    Replies: 4
    Last Post: 12-10-2010, 01:20 AM
  4. User Form Option & Command Buttons
    By Information Hog in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-19-2005, 06:37 PM
  5. Code Behind Buttons on User Form
    By robertguy in forum Excel General
    Replies: 0
    Last Post: 02-09-2005, 11:53 AM

Tags for this Thread

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