excel - Using a variable as part of a control name (to constantly check a condition) -


i using toggle buttons change color, caption , add / subtract counter. since have have few of buttons i'd rather habe general function instead of having change script each button.

this have:

private sub togglebutton1_click() if togglebutton1.value = true     togglebutton2.backcolor = rgb(0, 255, 0)     togglebutton2.caption = "pass"     range("a1").value = range("a1").value + 1      if range("a2").value > "0"         range("a2").value = range("a2").value - 1     end if    else     togglebutton1.backcolor = rgb(255, 0, 0)     togglebutton1.caption = "fail"     range("a2").value = range("a2").value + 1      if range("a1").value > "0"         range("a1").value = range("a1").value - 1     end if  end if  end sub 

what expect like:

for = 1 100     if togglebutton & i.value = true      .... 

or maybe each togglebutton blah

so how use variable in control name? tia

try adapting this

for integer = 1 12     cont = me.controls("chkcheckbox" & i)     if cont.value         msgbox cont.name & " true"     endif next 

Comments