excel - Handle many ComboBox_Change Event -


well i'm new in vba programming. i'm creating form helps me quotations, , there part of form shows items i've registered, this:

my form comboboxes

so purpose of comboboxes change or delete correponding item according option choose, , have lot of them in userform, making hard create many combobox event programs (like combobox1_change, combobox2_change, ... combobox50_change). , then, main question is: how in vba without loosing lot of time making same code different objects? create 1 code comboboxes.

i understand can in way below, i'm sure has better way do.

sub combobox1_change()   call mycode end sub  sub combobox2_change()   call mycode end sub  sub combobox50_change()   call mycode end sub  sub mycode() i=1 50    if controls("combobox" & i).value = "change"       call mysecondcode    end if next end sub 

i spent 30 minutes searching question, didn't find me. hope guys understood question. in advance.

update:

axel richter, said in comments, i'm having problem in this:

private function isnoktest()  if prod1.value = "" or _     prod2.value = "" or _     tecido.value = "" or _     tamanhos.value = "" or _     unitario.value = "" or _     quantidade.value = ""          isnoktest = true end if  end function   private mycbswithevents collection private sub userform_initialize()  set mycbswithevents = new collection  each c in me.controls   if left(c.name, 8) = "combobox"    c.additem "change"    c.additem "delete"    set mycbwithevents = new clscbwithevents    set mycbwithevents.mycb = c    mycbswithevents.add mycbwithevents   end if  next   end sub  ' ' ' 'datatext.value = format(now, "dd/mm/yyyy") 'bordadoqty.value = 1 'estampaqty.value = 1 'itemlab.caption = 1 ' 

when code added project, event in class module doesn't work, apparently isn't linked "events", don't know happened.

this can achieved using class module handles events.

insert class module in project. name clscbwithevents. in class module have following code:

public withevents mycb combobox  private sub mycb_change()  if me.mycb.value = "change"   msgbox me.mycb.name & " has changed ""change"""  elseif me.mycb.value = "delete"   msgbox me.mycb.name & " has changed ""delete"""  end if end sub 

in user form have following code:

private mycbswithevents collection  private sub userform_initialize()  set mycbswithevents = new collection  each c in me.controls   if typename(c) = "combobox"    c.additem "change"    c.additem "delete"    set mycbwithevents = new clscbwithevents    set mycbwithevents.mycb = c    mycbswithevents.add mycbwithevents   end if  next end sub 

now every combobox in user form use event handling.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -