I am not a VBA programmer, so I apologize in advance if some of my terminology in this matter is incorrect. My colleague wanted to clear the selection from the list as soon as it was selected. After some searches, we found that one way to do this was through the Change event. First we tried:
Private Sub ListBox1_Change() For i = 0 To ListBox1.ListCount - 1 ListBox1.Selected(i) = False Next i End Sub
However, it seemed that setting the Selected property to False raises the Change event in the list, and it actually becomes an infinite loop and causes Excel (2007) to crash. Given that we knew there were two entries, we also tried:
Private Sub ListBox1_Change() ListBox1.Selected(0) = False ListBox1.Selected(1) = False End Sub
And it works! Although we expect the same behavior - to set the Selected property, to raise the Change event to fire again and get an infinite loop.
However, it seems that once, for example, ListBox1.Selected (0) = False, the change event is restarted, but in this iteration it does not restart this line - I think because it knows that this selected for this element is already set to False , therefore nothing changes.
But if that is the case, we also expect this behavior in the first solution .. so it seems that there is some difference in saying ListBox1.Selected (i) = False compared to indicating the actual index of the element directly (and not through the variable i ).
Does anyone know the reason for this behavior? Hope the question makes sense, I tried to explain it as best as possible.
Thanks Amit
source share