excel vba - VBA Dependent Combobox -


i have 3 combo boxes dependent each other. list on cmb2 depends on value on cmb1. cmb3 list depends on cmb2 value.

my problem is, when remove/delete values on combo boxes, when click cmb2, still shows list of last value chosen on cmb1 & same goes cmb3.

what's code show empty if combo box it's dependent doesn't have value?

edit: think based on added information in comment should encapsulate logic in sub procedures , functions. best so, there should logic between named ranges , values in combobox.

in below example, created function handle conversions value in independent combobox , named ranges. can supply own connector, assumed space changed underscore (so, 'account information' replaced 'account_information').

next subprocedure checks confirm named range exists in workbook matches converted value in independent combobox. if so, dependent box takes on named range list property. if not, dependent combobox cleared.

the benefit using type of system repeated other comboboxes without rewriting code. see below code , let know if need added help.

private sub userform_initialize()     me.combobox1         .additem "account information"         .additem "other stuff"     end end sub private sub combobox1_change()     me         loaddependentcbo me.combobox1, me.combobox2     end end sub  private function convertvaluetonamedrange(svalue string, sconnector string) string     'this function takes values in combobox , converts them named ranges     'the spaces replaced whichever connector supplied function     'note: created separate function though uses built-in function     'in case more implementation code needed      convertvaluetonamedrange = replace(svalue, " ", sconnector) end function  private sub loaddependentcbo(cboindependent combobox, cbodependent combobox)     'this code looks see if there match between value of independent combobox     'and named range in workbook. if not, dependent combobox set empty      dim stemp string     dim rnamedrange range      stemp = ""     on error resume next         stemp = names.item(convertvaluetonamedrange(cboindependent.value, "_")).name     on error goto 0      if stemp = ""         'there no matching named range clear dependent combobox         cbodependent.clear     else         cbodependent.list = range(stemp).value     end if end sub 

old post: code combobox1.listindex = -1 set combo box empty (change name of box suit). logic depends on more details. let know if need implementation.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -