Userform Combobox default value

  • Hi, this is so simple but is killing me!
    I have a combo box called "account_list" and I have a dynamic range called "accounts" set in the row source property in vba.
    I would like to specify in the userform_initialize sub that one of the values in the list is default selected when the form loads.


    I have tried quite a few ways and none seem to work.


    Any ideas guys?


    Dave

  • Re: Userform Combobox default value


    here is my whole sub routine



  • Re: Userform Combobox default value


    Don't use the Value property to set the selected item in a ComboBox (or ListBox - essentially the same thing with the same Properties and Methods), use the ListIndex property.


    ListIndex is exactly what it says, the Index of the item in the list. It is 0 based, so if you really want the 9th item, then use 8 as the Listindex


    Code
    account_list.ListIndex = 8
  • Re: Userform Combobox default value


    The Value property is the text/value in the TextBox portion of the control - this may match an item in the list, but if the Style is set to '0 - fmStyleDropdownCombo', the user can enter something that does not match any item. You could set the Style to '1 - fmStyleDropdownList' which will only allow input that matches an item in the list.


    When working with comboboxes, it is better to explicitly query the selection rather than relying on the Text/Value properties.


    • ListIndex - Returns the Index of the selected item. ListIndex returns -1 if nothing is selected or the user has typed a non-matching string in the textbox. Listindex is 0 based
    • List(x) - Returns the text of the item at Index x. cb.List(cb.ListIndex) returns the text of the selected item (but will error if the ListIndex is -1).
    • ListCount - Returns the number of items in the list.



    You can also use

    • MatchFound - To determine if text entered in the textbox matches any item in the list
    • MatchRequired - Yes/No. If Yes then used to verify a valid selection has been made when the combobox loses focus. Focus is returned to the Combobox if not.
    • ListRows - To define how many items should be displayed in the dropdown (Nothing to do with your question really, but just putting it out there as it seems to be a very underused feature :))

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!