VBA- Dropown导航到工作表/工作簿

时间:2018-05-08 21:01:21

标签: excel vba excel-vba

Private Sub ComboBox1_Change()
  If ComboBox1.ListIndex > -1 Then Sheets(ComboBox1.Text).Select
End Sub
Private Sub ComboBox1_DropButtonClick()
    Dim xSheet As Worksheet
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If ComboBox1.ListCount <> ThisWorkbook.Sheets.Count Then
        ComboBox1.Clear
        For Each xSheet In ThisWorkbook.Sheets
            ComboBox1.AddItem xSheet.Name
        Next xSheet
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

这就是我到目前为止所拥有的。目前它显示我的所有工作簿/工作表,因为我只希望它显示10张特定的工作表。

2 个答案:

答案 0 :(得分:2)

基于此评论:

  

&#34;其他表格是A部分,B部分和D部分。我不希望它们进入   下拉&#34;

这将有效

    For Each xSheet In ThisWorkbook.Sheets
        Select Case xSheet.Name
            Case Is = "Part A", "Part B", "Part D"
            Case Else
                ComboBox1.AddItem xSheet.Name
        End Select
    Next xSheet

您可以轻松修改Select Case语句以创建更多条件。

您还可以执行更复杂的操作,如下面的更多模式匹配:

Select Case Instr(xSheet.Name, "(")
    Case Is > 0: ComboBox1.AddItem xSheet.Name
End Select

答案 1 :(得分:1)

你走了。

Private Sub ComboBox1_Change()
  If ComboBox1.ListIndex > -1 Then Sheets(ComboBox1.Text).Select
End Sub
Private Sub ComboBox1_DropButtonClick()
    Dim xSheet As Worksheet
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If ComboBox1.ListCount <> ThisWorkbook.Sheets.Count Then
        ComboBox1.Clear
        For Each xSheet In ThisWorkbook.Sheets
            If xSheet.Name Like "Part C (*" Then ComboBox1.AddItem xSheet.Name
        Next xSheet
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

交换机上的特定表名称:(由OP请求)

Private Sub ComboBox1_DropButtonClick()
    Dim xSheet As Worksheet
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If ComboBox1.ListCount <> ThisWorkbook.Sheets.Count Then
        ComboBox1.Clear
        For Each xSheet In ThisWorkbook.Sheets
            Select Case xSheet.Name
                Case "Part C (B)", "Part C (D)", "Part C (E)", "Part C (F)", "Part C (G)", _
                    "Part C (H)", "Part C (J)", "Part C (K)", "Part C (L)", "Part C (K)"
                    ComboBox1.AddItem xSheet.Name
            End Select
        Next xSheet
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

您可以添加,删除工作表名称,如果您决定破解该行,请不要忘记使用“_”符号来破解它。 (非常常见的错误)