Single delete button for 4 listbox each in different pages

60 Views Asked by At

In this question, the coding that i have done seem to be not working when i run the vba and click the delete button nothing happen. I don't know which part it error because it does not show any error to debug.

Private Sub cmdDelete_Click()
    Dim i As Long
    Dim selectedRows() As Long
    Dim count As Long
    Dim currentListBox As MSForms.listBox
    Dim currentPage As Long

    ' Determine the active ListBox on the current page
    currentPage = MultiPage1.Value

    ' Identify the active ListBox based on the current page
    Select Case currentPage
        Case 0
            Set currentListBox = ListBox1
        Case 1
            Set currentListBox = ListBox2
        Case 2
            Set currentListBox = ListBox3
        Case 3
            Set currentListBox = ListBox4
        Case Else
            ' Handle any other cases if needed
    End Select

    ' Check if a ListBox was identified
    If currentListBox Is Nothing Then
        MsgBox "Please ensure a ListBox is selected.", vbExclamation
        Exit Sub
    End If

    ' Determine the selected rows in the identified ListBox
    count = 0
    For i = 0 To currentListBox.ListCount - 1
        If currentListBox.Selected(i) Then
            ReDim Preserve selectedRows(count)
            selectedRows(count) = i
            count = count + 1
        End If
    Next i

    ' Delete the selected rows in reverse order
    For i = UBound(selectedRows) To LBound(selectedRows) Step -1
        ' Use the current ListBox's Tag property to determine which ListBox to delete from
        Select Case currentListBox.Tag
            Case "1"
                ' Delete rows from ListBox1 on Page 1
                MultiPage1.Pages(0).ListBox1.RemoveItem selectedRows(i)
            Case "2"
                ' Delete rows from ListBox2 on Page 2
                MultiPage1.Pages(1).ListBox2.RemoveItem selectedRows(i)
            Case "3"
                ' Delete rows from ListBox3 on Page 3
                MultiPage1.Pages(2).ListBox3.RemoveItem selectedRows(i)
            Case "4"
                ' Delete rows from ListBox4 on Page 4
                MultiPage1.Pages(3).ListBox4.RemoveItem selectedRows(i)
        End Select
    Next i
End Sub 

Here is the coding that i have done and stuck with this delete button.

2

There are 2 best solutions below

0
FaneDuru On

You did not answer the clarification question... So, my above adapted code is able to remove the selected rows in the active list box or in all list boxes (according to boolAllLstBoxes boolean variable):

Private Sub CommandButton1_Click()
   Const boolAllLstBoxes As Boolean = True 'true to delete selection in ALL LIST BOXES
   Dim currentListBox As MSForms.listbox
   Set currentListBox = Me.Controls("ListBox" & Me.MultiPage1.value + 1)

   Debug.Print currentListBox.ListIndex
   
   ' Determine the selected rows in the identified ListBox
    Dim i As Long, count As Long, selectedRows()
    ReDim selectedRows(currentListBox.ListCount)
    
    For i = 0 To currentListBox.ListCount - 1
        If currentListBox.Selected(i) Then
            selectedRows(count) = i: count = count + 1
        End If
    Next i
    If count = 0 Then
        MsgBox "No any selection in " & currentListBox.name
        Exit Sub
    Else
        ReDim Preserve selectedRows(count - 1)
    End If
    
    'remove selected rows in all list boxes or in the active one (acc boolAllLstBoxes):
    If boolAllLstBoxes Then
        'to delete the array rows from all list boxes:
        For i = UBound(selectedRows) To LBound(selectedRows) Step -1
            currentListBox.RemoveItem selectedRows(i)
            For count = 2 To 4
                Me.Controls("ListBox" & count).RemoveItem selectedRows(i)
            Next count
        Next i
    Else
        'to delete ONLY the selected lines:
        For i = UBound(selectedRows) To LBound(selectedRows) Step -1
            currentListBox.RemoveItem selectedRows(i)
        Next i
    End If
End Sub

The way the above code uses for ReDim Preserve is better in terms of Memory handling...

If you need a piece of code able to delete rows (only) in the list box of the active page, the above code can be considerably simplified.

Please, send some feedback after testing it.

0
user3598756 On

Your wording isn't completely clear to me

One guess out of some is you could simply go:

Private Sub CommandButton1_Click()

    Dim lb As MSForms.ListBox
        With Me
            Set lb = .Controls("Listbox" & .MultiPage1.Value + 1)
        End With

            With lb
                Dim i As Long
                    For i = .ListCount - 1 To 0 Step -1
                        If .Selected(i) Then
                            .RemoveItem i
                        End If
                    Next
            End With
End Sub