d_and_a_kate
d_and_a_kate

Reputation: 39

When adding/removing last value from one listbox to another, whole listbox clears?

I am creating a userform (riskcodefiter) where the user can move values over from one list box to another. The first list box allriskcodes contains many values (populated from a range on another worksheet), and the second list box chosenriskcodes will contain the values from allriskcodes the user wants to use as a filter. The userform includes Btn_addallcodes, Btn_removeallcodes, Btn_addcodes, Btn_removecodes - which are self explanatory.

The problem: If there are multiple values in the chosenriskcodes listbox, and the user wants to remove the last value in the box, it will clear the entire chosenriskcodes listbox. This is also the case for my Btn_addcodes. Any advice?

This is the code I have for my Btn_removecodes

Private Sub BTN_removecodes_Click()
     Dim iCtr As Long

    For iCtr = 0 To Me.chosenriskcodes.ListCount - 1
        If Me.chosenriskcodes.Selected(iCtr) = True Then
           Me.allriskcodes.AddItem Me.chosenriskcodes.List(iCtr)
        End If
    Next iCtr

    For iCtr = Me.chosenriskcodes.ListCount - 1 To 0 Step -1
        If Me.chosenriskcodes.Selected(iCtr) = True Then
           Me.chosenriskcodes.RemoveItem iCtr
        End If
    Next iCtr
End Sub

This is the code I have for my Btn_addcodes

    Private Sub BTN_addcodes_Click()
    Dim iCtr As Long

    For iCtr = 0 To Me.allriskcodes.ListCount - 1
        If Me.allriskcodes.Selected(iCtr) = True Then
            Me.chosenriskcodes.AddItem Me.allriskcodes.List(iCtr)
        End If
    Next iCtr

    For iCtr = Me.allriskcodes.ListCount - 1 To 0 Step -1
        If Me.allriskcodes.Selected(iCtr) = True Then
            Me.allriskcodes.RemoveItem iCtr
        End If
    Next iCtr

    End Sub

The button works fine for all the other values in the list boxes, except for the last value. Any ideas?

Upvotes: 1

Views: 65

Answers (1)

CLR
CLR

Reputation: 12279

You can see the issue happening if you insert breakpoints and watch what happens..

If there are say, 50 items in allriskcodes and the 50th item is selected, deleting it causes the 49th item to become selected (as if it's an active cursor).

The simple workaround is to deselect each item just before you delete it like so:

Private Sub BTN_addcodes_Click()
    Dim iCtr As Long

    For iCtr = 0 To Me.allriskcodes.ListCount - 1
        If Me.allriskcodes.Selected(iCtr) = True Then
            Me.chosenriskcodes.AddItem Me.allriskcodes.List(iCtr)
        End If
    Next iCtr

    For iCtr = Me.allriskcodes.ListCount - 1 To 0 Step -1
        If Me.allriskcodes.Selected(iCtr) = True Then
            Me.allriskcodes.Selected(iCtr) = False
            Me.allriskcodes.RemoveItem iCtr
        End If
    Next iCtr

End Sub

Also, to make this more readable, you could use the With statement:

Private Sub BTN_addcodes_Click()

    Dim iCtr As Long
    With Me.allriskcodes

        For iCtr = 0 To .ListCount - 1
            If .Selected(iCtr) = True Then
                Me.chosenriskcodes.AddItem .List(iCtr)
            End If
        Next iCtr

        For iCtr = .ListCount - 1 To 0 Step -1
            If .Selected(iCtr) = True Then
                .Selected(iCtr) = False
                .RemoveItem iCtr
            End If
        Next iCtr

    End With

End Sub

Upvotes: 1

Related Questions