+ Reply to Thread
Results 1 to 6 of 6

How to deal with multiple sets of code in the Initialize Event of a userform

  1. #1
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    How to deal with multiple sets of code in the Initialize Event of a userform

    As we know, the Initialize Event of a userform is a very 'busy person', meaning for large and maybe complicated userforms with lots of things happening in them, we may need to equally have lots of things happening in the Initialize Event of a userform.

    I am illustrating an example below in a 'less busy' userform but with a problem that can be encountered in a busier form with more data:

    Below you will see the existing code in a userform, which essentially first displays a welcome message and then shows a userform:

    ini1.png


    Some new data needs to be added to the Initialize Event for the combobox function as shown below:

    ini2.png

    However the below error is received:

    ini3.png

    But the error is gone when the code is placed as below:

    ini4.png

    I have tried separating both codes with End Sub and End With etc. but no luck.

    My question is, how are several codes in the Initialize Event usually separated (for example in this current example) rather than piling them on top of each other like I did to achieve resolution here? Some folks may go the other way and create an alternative way to code the comboboxes without using the Initialize Event, but in my opinion, running from matters like those gets rid of the symptoms, but the illness is still there.

    The code is below and the workbook is attached. I appreciate your advice.



    Please Login or Register  to view this content.
    Attached Files Attached Files

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

    Re: How to deal with multiple sets of code in the Initialize Event of a userform

    Use a separate routine to store the code and call that in the event routine.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    12-18-2015
    Location
    Denmark
    MS-Off Ver
    2010
    Posts
    1

    Re: How to deal with multiple sets of code in the Initialize Event of a userform

    You can't ever put code outside subs or functions, unless it's a global definition.

    You can add a sub, just for adding items to your combobox, and make it prettier by using the 'with' statement. If there is a lot of data, you could move it over to its own module, so its not visible in the code for UserForm1.

    Please Login or Register  to view this content.
    EDIT: Same as Andy said, i was too slow
    Last edited by peterspliid; 12-18-2015 at 05:14 AM.

  4. #4
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: How to deal with multiple sets of code in the Initialize Event of a userform

    Quote Originally Posted by Andy Pope View Post
    Use a separate routine to store the code and call that in the event routine.

    Please Login or Register  to view this content.

    Thanks guys. Both worked.

    Andy, however in the below picture, when I place the cursor on the code in the Private Sub m_LoadComboBox(), the procedure window as shown by the red arrow does not say "Initialize" but says "m_LoadComboBox". Is it correct to say that the m_LoadComboBox still falls inside or within or under the overall "Initialize' umbrella, hence enabling the combobox to be populated when the userform is Initialized?

    ini6.png

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

    Re: How to deal with multiple sets of code in the Initialize Event of a userform

    The code is called by the m_LoadComboBox line in the initialize event.
    So it routes out to execute that code and then returns to the initialize event and contines to execute those commands, such as the msgbox

  6. #6
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: How to deal with multiple sets of code in the Initialize Event of a userform

    Quote Originally Posted by Andy Pope View Post
    The code is called by the m_LoadComboBox line in the initialize event.
    So it routes out to execute that code and then returns to the initialize event and contines to execute those commands, such as the msgbox
    Thank you Andy, you good man. I understand this nicely now. Good day to you

+ 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] Userform, multiple sets of ToggleButtons, rationalise to single code?
    By poolierob in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-13-2015, 12:15 PM
  2. How do I source and initialize data from multiple sheets with one VBA code?
    By mbizavin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-28-2014, 04:55 PM
  3. Merge two sets of Change event code
    By Nitefox in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 2
    Last Post: 11-25-2013, 12:18 AM
  4. Initialize event to populate two Comboboxes?
    By fcb1900 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-21-2010, 05:01 PM
  5. userform event handling within a standard code module?
    By jerseyguy1996 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2010, 01:01 PM
  6. User Form Initialize Event Issues
    By Randy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-04-2005, 07:30 AM
  7. UserForm Initialize event doesn't fire reliably
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-15-2005, 09:41 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