+ Reply to Thread
Results 1 to 6 of 6

Event for when ANY combobox changes in a userform

  1. #1
    Registered User
    Join Date
    09-30-2013
    Location
    Nelson Bay, Australia
    MS-Off Ver
    Excel 2003
    Posts
    28

    Event for when ANY combobox changes in a userform

    Background:
    I have a userform where the user adds details of a document transfer (who the docs are going to, the date etc). The user selects the document title from a combobox, which is a list of all of the documents in the document register. Some other corresponding "disabled" textboxes fill with the various attributes of the selected document, based on the data in the register. This is all good so far...

    The thing I need to also consider is that the user might want to send multiple (up to 20) documents (each with their own set of attributes) per transfer. I'm achieving this by adding 20 instances of the document title combobox and the associated textboxes. All of the doc title comboboxes are named "Title1" through to "Title20". Same naming scheme for the attribute text boxes. On userform_initialise, 19 sets of boxes are hidden (visible=false) with a simple loop.

    There is a button on the form to add another doc. Clicking it simply unhides the next set of boxes, so the user can populate. This is the best method to display a variable amount of boxes, that I could come up with.

    Assistance needed:
    I need all 20 of my document title comboboxes to have a _change event, which will be almost identical: get the list.index of changed combobox and use that number to refer to the corresponding attribute in the doc register and bring that data into the form.
    My question is: is there a way to make a sub that triggers when ANY combobox changes? If so, can I do that in a way that can get the name of the changed combobox, so I can get the number of it, to define which attribute boxes will update?

    Just trying to avoid having 20 virtually identical _change events on the userform. They would all use the same public variables though, so not really a memory concern (if that would even be an issue).

    Is there a completely different/better way of achieving this that I haven't thought of?

    Thanks in advance.

    Note- before anyone asks, I know it's not really conducive to getting the best assistance possible, but I'm not in a position to be able to upload a copy of the document due to restrictions on my organisation's network. Sorry!

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,002

    Re: Event for when ANY combobox changes in a userform

    Try using a class event hander.

    Create a Class Module called clsCombos and add the following code.


    Please Login or Register  to view this content.
    In your userform
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-30-2013
    Location
    Nelson Bay, Australia
    MS-Off Ver
    Excel 2003
    Posts
    28
    Quote Originally Posted by ByteMarks View Post
    Try using a class event hander.

    Create a Class Module called clsCombos and add the following code.


    Please Login or Register  to view this content.
    In your userform
    Please Login or Register  to view this content.
    I really appreciate this. I had an inkling I could do this with class modules, but I haven't used them before and was a bit apprehensive. I think what you've shown me here makes great sense so I'll give it a go and come back. Thanks again.

  4. #4
    Registered User
    Join Date
    09-30-2013
    Location
    Nelson Bay, Australia
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Event for when ANY combobox changes in a userform

    Hi,

    This worked exactly as advertised, thank you. This passes the ComboBox details into the Class Module for use as required. The next step of what I needed to do was use the changed clsCombos.ListIndex to update some other TextBoxes in the UserForm.
    With my simple brain, I tried to do something like:
    Please Login or Register  to view this content.
    This worked (UserForm.TextBox1 updated with the expected data) when I manually initialized the UserForm from within VBA. It DID NOT work when I ran it via the button on the spreadsheet that initializes a new UserForm and displays it (with .show). What seemed to happen was that when the Userform.Controls("TextBox1").text = Worksheets("Sheet1").Range("A:" 2 + clsCombos.ListIndex).value line would run in the Class Module, it would run the initialize script on the UserForm again. Is this expected behaviour? The only difference I can see is the addition of the .show step. Why does that re-initialize the form when I try? Can I do anything about that?

    The Googling I've done makes me think another solution is something to do with setting up a Property in the Class Module which can then be "Got" by the UserForm. Am I on the right track? Could you please explain how this works, or give me an example of the code I would need?

  5. #5
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,386

    Re: Event for when ANY combobox changes in a userform

    Or a bit shorter:

    This in the class

    Please Login or Register  to view this content.
    This in the userform initialize

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,002

    Re: Event for when ANY combobox changes in a userform

    Worksheets("Sheet1").Range("A:" 2 + clsCombos.ListIndex).value
    Is that a valid range?

+ 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] Which event to populate UserForm combobox
    By MicrobicTomb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2023, 09:33 AM
  2. [SOLVED] Multi combobox and class event on userform
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-18-2022, 10:26 AM
  3. Replies: 2
    Last Post: 01-22-2020, 10:17 AM
  4. [SOLVED] UserForm ComboBox event to trigger MsgBox
    By IGNF in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2018, 04:50 AM
  5. [SOLVED] I need a simple code for a click event using a UserForm ComboBox
    By Pyro Form in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-17-2012, 07:58 AM
  6. [SOLVED] UserForm ComboBox Change Event Question
    By tja1964 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2012, 07:21 PM
  7. MsgBox in Enter event causes combobox not to run Change event
    By Richard in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-06-2006, 10:55 AM

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