Munkeeface
Munkeeface

Reputation: 409

Why is my listbox removing all items instead of what's selected?

I have a set of ListBoxes that pulls data from a report that the rest of the encompassing macro generates. These ListBoxes are displayed in a UserForm adjacent to one another to compare their data together and allow the user to remove data if it matches (refer to picture). Unfortunately, when I press the remove button for the right list (BWListBox), the entire list gets removed rather than just a single item.

I suspect this is due to the dynamic nature of the ListBox (if a Bank Wire report is generated, the list remains single-select and single-column; if a Credit Card Report is generated, it changes to multi-select and double-column, as well as marking a "Total" TextBox as visible, just as the left side)

When I run the Credit Card Report, and use the mark the list (while it's multi-select), the remove button works just fine. Oddly enough, in the single-select form, it removes the entire list.

Here is the block of code for the Remove Button:

Private Sub RemoveButton2_Click()
    Dim bwTotal As Double
    'If CCReport Then              `Currently not functioning due to testing
        'bwTotal = BWTotalTextBox.Value 'Throwing error - Double to String ""
    'End If
    For lItem = BWListBox.ListCount - 1 To 0 Step -1
        If BWListBox.Selected(lItem) Then
            'GPListBox.RemoveItem GPListBox.ListIndex
            BWListBox.RemoveItem lItem
            If CCReport Then       'if CCReport since it will  be multi-select and multi-column 
                bwTotal = bwTotal - BWListBox.List(lItem, 1)
                BWTotalTextBox.Value = Format(bwTotal, "$#,##0.00")
            End If
        End If
    Next
End Sub

Here is the BWListBox_Change event:

Private Sub BWListBox_Change()
    If CCReport Then   'This only exists for the sole purpose of the BWTotalTextbox, if it's not visible (Such as during a Bank Wire Report), there is no need
        Dim bwTotal As Double

        For lItem = 0 To BWListBox.ListCount - 1
            If BWListBox.Selected(lItem) = True Then
                bwTotal = bwTotal + BWListBox.List(lItem, 1)
                Debug.Print BWListBox.List(lItem, 1)
            End If
        Next

        BWTotalTextBox.Value = Format(bwTotal, "$#,##0.00")
    End If
End Sub

EDIT: I recently found it only removes the entire list if the LAST item is selected prior to pressing the remove button.

enter image description here

Upvotes: 1

Views: 762

Answers (1)

cyboashu
cyboashu

Reputation: 10433

In case of single select, the loop keeps on running because when you delete one item at the bottom, the immediate item above that gets selected and the loop gets validated.

So, all you need to do is jump out of the loop after deleting the selected element.

For lItem = Me.ListBox1.ListCount - 1 To 0 Step -1
        If ListBox1.Selected(lItem) Then
            ListBox1.RemoveItem lItem                
            If Me.ListBox1.MultiSelect = fmMultiSelectSingle Then
                Exit For
            End If
        End If
Next

Upvotes: 3

Related Questions