excel 单个删除按钮4列表框每个在不同的页面

fdbelqdn  于 2023-11-20  发布在  其他
关注(0)|答案(2)|浏览(148)

在这个问题中,我所做的编码似乎不工作,当我运行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

字符串
这是我做的编码,并坚持这个删除按钮。

kognpnkq

kognpnkq1#

你没有回答澄清问题.所以,我上面的改编代码能够删除活动列表框或所有列表框中的选定行(根据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的方式在内存处理方面更好。
如果你需要一段代码,能够删除行(只)在列表框中的活动页面,上述代码可以大大简化。
请在测试后发送一些反馈。

juzqafwq

juzqafwq2#

你的措辞对我来说不是很清楚
一个猜测是,你可以简单地说:

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

字符串

相关问题