excel - Why cant select variables from listbox like this in VBA? -
hey guys want select variable "variable(s)" listbox "row" and/or "column" listbox.
i know should write this:
irow = lbxvar.listcount 1 step -1 if lbxvar.selected(irow - 1) = true lbxcolumn.additem lbxvar.list(irow - 1) lbxvar.removeitem (irow - 1) end if next irow
i don't understand why cannot write code this?
if lbxvar.listindex > -1 irow = 0 lbxvar.listcount - 1 if lbxvar.selected(irow) = true lbxcolumn.additem lbxvar.list(irow) lbxvar.removeitem (irow) end if next irow end if
it showed error:
thanks you.
the reason why have loop on collection backwards because when remove item list, lbxvar.listcount
gets smaller.
however, in loop, number of iterations fixed after begins executing - expression lbxvar.listcount - 1
evaluated once. happens if items removed, overrun bounds of lbxvar.selected
.
when loop backwards don't have problem, because changes indexes of items you've iterated over. if order added second listbox trying preserve going through index forward instead of backward, you'll have loop on selected items twice - once copy other listbox, , once delete them:
if lbxvar.listindex > -1 'add pass: irow = 0 lbxvar.listcount - 1 if lbxvar.selected(irow) lbxcolumn.additem lbxvar.list(irow) end if next irow 'remove pass: irow = lbxvar.listcount 1 step -1 if lbxvar.selected(irow - 1) lbxvar.removeitem (irow - 1) end if next irow end if
Comments
Post a Comment