Combo Box Items not sticking

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,148
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
In Workbook Opens event I'm adding items to some combo boxes.
When the Form is opened the items are there. Good.
But if I close the Form and open it again, they're gone.
I didn't expect this. It is normal behaviour or am I missing something?
 
Yes, this is normal behavior. You have combobox loading and form opening in the Workbook_Open event. On the other hand, by launching the form with, for example, the F5 key, you only open the form (combobox loading “stayed” in Workbook_Open).
You can use this code construction:
1. In the ThisWorkbook module:
VBA Code:
Private Sub Workbook_Open()
    Call RunMyForm
End Sub
2. In the standard module:
VBA Code:
Sub RunMyForm()
    With UserForm1
        .ComboBox1.List = Worksheets("Sheet1").Range("A1:A5").Value
        
        .Show
    End With
End Sub

If you want to reopen the form along with loading the combobox you run the RunMyForm procedure.

Artik
 
Upvote 0
Thanks for confirming that @Artik. I'm trying some new (for me) stuff. I presume the same apples to any data in Form controls,. It will need to be re-applied every time the form is opened?
My aim is not to show the Form from Workbook open, just prepare it to Open with data loaded.
 
Upvote 0
It sounds like you should probably be using the Userform_Initialize event for your code then.
 
Upvote 0
Thanks @RoryA, I'll try that and see results.

The Form holds various options like some Form properties and file paths.
They're kept in a db Table with fields for default values and user-set values.
In workbook open, Excel connects to the DB and loads user values, or if they're missing the default values into a class.
This lets me see/use them anywhere in the workbook. The User can opens the Form, see the data and make any changes This seem ok so far?

I've no example of this sort of this so have to work it out with trial and error. If something was changed I was updating the source db, but realised this could mean, for a temporary change, a later restore. My plan now is to try is update the db only in workbook before close, comparing the initial values against the current class ones.
So I think what you're advising is use Form initialize to write the values (from the class), and they'll be present when the Form is opened. When closed it compares/updates the class.

What I'm seeing is the Form not showing data, perhaps it must be restored first before Open.
I tried setting a Form Object, but that got an error about losing something, so guess that's not the right approach.

Will plug on with Initialize.. all good fun, thanks.
 
Upvote 0

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