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.
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
boolAllLstBoxesboolean variable):The way the above code uses for
ReDim Preserveis 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.