+ Reply to Thread
Results 1 to 9 of 9

Assign Click Action to Dynamic Button on Userform

  1. #1
    Registered User
    Join Date
    07-12-2018
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    4

    Assign Click Action to Dynamic Button on Userform

    I have a code that will populate a Userform with buttons and caption them based on a list of names in a worksheet:

    Excel.PNG

    Essentially what I'm trying to make happen is that when this Userform loads, the user can click any of the buttons and it will save the caption of the selected button to a global string variable 'BtnCaption', hide Userform1 and show Userform2. I've tried to look up how to do this but everything I found didn't really make sense or didn't work with what I'm doing.

    Here's my code in Userform1 for creating the button if that helps:

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Assign Click Action to Dynamic Button on Userform

    .
    Is this what you were attempting ?

    Please Login or Register  to view this content.
    Paste the above into the UserForm1 UserForm_Initialize macro.

    Create three more userforms, #2, #3 & #4.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    07-12-2018
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    4

    Re: Assign Click Action to Dynamic Button on Userform

    No, I've been able to do something like what you made. The thing is that my code from above will populate a Userform with buttons based on data in the rows from column A so if it's run with what I have from above it'll show the Userform with 3 buttons, but for example if I run it again after adding 'DDD' to the button names it will create a Userform like this:

    Excel2.PNG

    So I can't just write code for 3 command button clicks because I don't know how many buttons will be on the Userform. Also, I want Userform2 to load no matter which option is picked because it's going to ask the same question next, I just need the option from Userform1 to be stored as a variable somehow and I'm trying to make it work with buttons rather than a Listbox.

    Here's my excel file in case that helps:

    Dynamic Button Test.xlsm

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Assign Click Action to Dynamic Button on Userform

    .
    My apologies .. this is not something I can get my head around. Hopefully another Forum Member will be able to assist.

  5. #5
    Registered User
    Join Date
    07-12-2018
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    4

    Re: Assign Click Action to Dynamic Button on Userform

    No worries, thanks anyway!

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Assign Click Action to Dynamic Button on Userform

    Try this for starters.
    Userform1 code.
    Please Login or Register  to view this content.
    ClassModule code (called Class1)
    Please Login or Register  to view this content.
    Userform2 code.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

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

    Re: Assign Click Action to Dynamic Button on Userform

    Suggestion of an alternative approach.

    Load the contents of column A into a listbox and have 1 button that deals with a click. The list box will allow for scrolling when the number of entries becomes large.
    Cheers
    Andy
    www.andypope.info

  8. #8
    Registered User
    Join Date
    07-12-2018
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    4

    Re: Assign Click Action to Dynamic Button on Userform

    Thanks bakerman2! This is exactly what I needed, I figured it might involve a class module but I don't have much experience using them.

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Assign Click Action to Dynamic Button on Userform

    Glad to help.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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] Assign macro to a dynamic button using vba
    By hanif in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-11-2016, 04:26 AM
  2. [SOLVED] Creating a userform when ı click the dynamic command button?
    By END93 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-17-2016, 06:42 PM
  3. Button Click on Spreadsheet commanded by Button Click on Userform
    By Quilie19 in forum Excel Programming / VBA / Macros
    Replies: 33
    Last Post: 04-07-2015, 11:35 PM
  4. Dynamic Button based on Slide Bar Action
    By DM2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-10-2015, 04:13 PM
  5. [SOLVED] Userform button action - clear,copy paste and print crashes. Please help with code
    By intex in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-05-2014, 12:16 PM
  6. vba to have command button click assign a number to another userform
    By lewisw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-23-2013, 09:12 AM
  7. Assign menu action to userform button
    By Yappa in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-22-2008, 10:58 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