Contain all events within userform_initalize

Prevost

Board Regular
Joined
Jan 23, 2014
Messages
198
Hi There. Is there a way to control what happens when a button is clicked under the userform_initialize event? I am not sure I understand the hierarchy of how to properly create a Userform. I have public variables declared in a module and need to use those values in the dropdown box menu, but I do not want to have a bunch of bricks of code such as CommandButton1_Click() or TextBox1_Change() that each declare their own varibles based on the public variables (as I will end up repeating myself in the userform events).
Thanks!

Code:
Private Sub UserForm_Initialize()

    'code here to declare variables
    
    Load values into combobox dropdown list     'this is what I am unsure about
    
     When a button is clicked...                      'this is what I am unsure about
        'do this code

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi Prevost,

The UserForm_Initialize event procedure should be used to do initial setup of the form prior to it being made visible to the user.

There isn’t a way to move the event procedures for the UserForm's controls into the UserForm_Initialize procedure, and even if there were, that wouldn’t be desirable.

The concern you’re trying to address – avoiding unnecessary duplication of code – is a valid one. There are techniques that can help do that while keeping your code robust and adaptable.

Having many short event procedures (“bunch of bricks” :) ) in UserForm code module is a normal result of good application design and coding. But rather than having code duplicated in these procedures, it’s a better practice to make Calls to Subs and Functions that many event procedures use in common.

You note that you are using global variables in your UserForm code. A best practice in VBA coding is to minimize the scope of variables unless there’s a valid reason to do otherwise. Typically, global variables (that have scope outside your UserForm) should be avoided. The use of module-scope variables (that can be shared by procedures within the UserForm code module) can useful.

If you’ll post an example of your code, it will be easier to provide you with specifics on how to apply these techniques to your application.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top