vendredi 6 avril 2018

VBA "All" checkbox with listbox deselect loop issues

I have a check box that is used for "ALL" (selected by default) selections in a muliselect listbox that is deselected when an item in the listbox is selected. I also coded it so when "ALL" is selected, then it clears the listbox selections and checks the box. It ends up looping through the different subs and makes it annoying for the user.

For instance, when I click an item in the listbox it selects that value, then deselects the checkbox. Since the checkbox is deselected, it goes back through the listbox and deselects the selected item. It loops between the two subs a couple times and ends up only working correctly half the time.

Can I prevent entering the other sub? Is there better logic so it won't loop as it does? or maybe an better method to achieve this?

Multiselect listbox:

Private Sub Mkts_Change()

If Me.cheMkts.Value = True Then
Me.cheMkts.Value = False
End If

End Sub

Checkbox:

Private Sub cheMkts_Click()
Dim i As Integer

For i = 0 To Mkts.ListCount - 1
If Me.Mkts.Selected(i) = True Then
    Me.Mkts.Selected(i) = False
End If
Next
End Sub

Aucun commentaire:

Enregistrer un commentaire