提问人:Khai 提问时间:11/1/2023 最后编辑:braXKhai 更新时间:11/2/2023 访问量:49
单个删除按钮用于 4 个列表框,每个列表框位于不同的页面中
Single delete button for 4 listbox each in different pages
问:
在这个问题中,当我运行 vba 并单击删除按钮时,我所做的编码似乎不起作用,什么也没发生。我不知道它出错的是哪一部分,因为它没有显示任何要调试的错误。
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
这是我完成的编码,并坚持使用此删除按钮。
答:
1赞
FaneDuru
11/1/2023
#1
你没有回答澄清问题......因此,我上面改编的代码能够删除活动列表框或所有列表框中的选定行(根据布尔变量):boolAllLstBoxes
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
上面代码的使用方式在内存处理方面更好......ReDim Preserve
如果您需要一段能够(仅)删除活动页面列表框中的行的代码,则可以大大简化上述代码。
请在测试后发送一些反馈。
0赞
user3598756
11/1/2023
#2
你的措辞对我来说并不完全清楚
从一些人中可以猜测的是,您可以简单地去:
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
评论