使用列表框中的复选框隐藏/取消隐藏工作表

时间:2020-04-07 15:43:44

标签: excel vba

Screenshot1

基于如下所示的当前代码(向Samuel Everson大喊大叫),我能够自动在列表框下创建复选框(如屏幕截图所示)。但是,复选框是否有可能隐藏/取消隐藏选项卡?因此,如果我勾选 Warehouse 1 ,它将显示Warehouse 1的标签,并在我取消选中该框时将其隐藏;等

现在的问题是,我真的不知道如何从这里开始。任何帮助将是巨大的!在此先感谢。 :)


Dim WorksheetArray() As Variant
Dim WorksheetIndex As Long
Dim ArrayElement As Variant
Dim NumberOfSheets As Long

NumberOfSheets = ThisWorkbook.Sheets.Count
ReDim WorksheetArray(1 To NumberOfSheets)

For WorksheetIndex = 1 To UBound(WorksheetArray)
    WorksheetArray(WorksheetIndex) = ThisWorkbook.Sheets(WorksheetIndex).Name
Next WorksheetIndex

ThisWorkbook.Sheets("CheckBoxSheet").WarehouseListBox.Clear
ThisWorkbook.Sheets("CheckBoxSheet").ShippingListBox.Clear
ThisWorkbook.Sheets("CheckBoxSheet").QualityListBox.Clear
ThisWorkbook.Sheets("CheckBoxSheet").SecurityListBox.Clear

For Each ArrayElement In WorksheetArray()
    If ArrayElement Like "Warehouse*" Then

    ElseIf ArrayElement Like "Shipping*" Then
        ThisWorkbook.Sheets("CheckBoxSheet").ShippingListBox.AddItem ArrayElement

    ElseIf ArrayElement Like "Quality*" Then
        ThisWorkbook.Sheets("CheckBoxSheet").QualityListBox.AddItem ArrayElement

    ElseIf ArrayElement Like "Security*" Then
        ThisWorkbook.Sheets("CheckBoxSheet").SecurityListBox.AddItem ArrayElement

    End If

Next ArrayElement

End Sub


2 个答案:

答案 0 :(得分:3)

这是一个简单的示例,您可以调整以适合您的特定设置。

Private Sub ListBox1_Change()

Dim i As Long

For i = 0 To Me.ListBox1.ListCount - 1
    Worksheets(Me.ListBox1.List(i)).Visible = Me.ListBox1.Selected(i) 'true if selected
Next i

End Sub

您可能还需要检查至少可见一张纸。

答案 1 :(得分:1)

基本上,对我发布的问题中提到的所有其他4个列表框进行重复。

Private Sub WarehouseListBox_Change()
    Dim i As Integer

    For i = 0 To Me.WarehouseListBox.ListCount - 1
        If Me.WarehouseListBox.Selected(i) Then
            Worksheets(Me.WarehouseListBox.List(i)).Visible = True
        Else
            Worksheets(Me.WarehouseListBox.List(i)).Visible = False
        End If
    Next i
End Sub