我想使用1个组合框来导航许多引用组合框包含的项目的工作表

时间:2019-04-10 04:45:44

标签: excel vba

我有1个ComboBox1包含5个项目。有1,2,3,4和5,它们分别代表Sheet1,Sheet2,Sheet3,Sheet4和Sheet 5。如果我在ComboBox中选择了项1,那么我单击CommandButton1,它将显示Sheet1。如果选择项目2、3、4或5,则将相同,分别显示Sheet2,Sheet3,Sheet4和Sheet 5。

问题是我只想使用1个CommandButton来导航这些工作表

Private Sub CommandButton1_Click()
    Dim a As Integer
    a = 3
If Not IsNumeric(TextBox2.Value) Then
    MsgBox "Only numbers allowed", vbCritical
    TextBox2.Text = ""
    TextBox2.SetFocus

ElseIf Me.ComboBox1.Value = "" Then
    MsgBox "Please Select the Stencil ID", vbCritical
    ComboBox1.Text = ""
    ComboBox1.SetFocus

ElseIf ComboBox1.Enabled = True Then
    a = WorksheetFunction.CountA(Sheet10.Range("a3:a1000000"))
    a = a + 3
    Sheet10.Cells(a, 1) = TextBox2.Text
    Sheet10.Cells(a, 2) = ComboBox1.Text
    Sheet10.Cells(a, 3) = DTPicker1.Value
    Sheet10.Cells(a, 4) = TextBox1.Text

ElseIf ComboBox1.Text = "1" Then
    ThisWorkbook.Sheets("Sheet1").Visible = True
    ThisWorkbook.Sheets("Sheet1").Select
    ThisWorkbook.Sheets("Sheet1").Range("a1").Select

ElseIf ComboBox1.Text = "2" Then
    ThisWorkbook.Sheets("Sheet2").Visible = True
    ThisWorkbook.Sheets("Sheet2").Select
    ThisWorkbook.Sheets("Sheet2").Range("a1").Select

ElseIf ComboBox1.Text = "3" Then
    ThisWorkbook.Sheets("Sheet3").Visible = True
    ThisWorkbook.Sheets("Sheet3").Select
    ThisWorkbook.Sheets("Sheet3").Range("a1").Select

End If

End Sub

1 个答案:

答案 0 :(得分:0)

尝试像这样安排代码:

If Not IsNumeric(TextBox2.Value) Then
    MsgBox "Only numbers allowed", vbCritical
    TextBox2.Text = ""
    TextBox2.SetFocus

ElseIf Me.ComboBox1.Value = "" Then
    MsgBox "Please Select the Stencil ID", vbCritical
    ComboBox1.Text = ""
    ComboBox1.SetFocus
end if

If ComboBox1.Enabled = True Then
    a = WorksheetFunction.CountA(Sheet10.Range("a3:a1000000"))
    a = a + 3
    Sheet10.Cells(a, 1) = TextBox2.Text
    Sheet10.Cells(a, 2) = ComboBox1.Text
    Sheet10.Cells(a, 3) = DTPicker1.Value
    Sheet10.Cells(a, 4) = TextBox1.Text
end if

If ComboBox1.Text = "1" Then
    ThisWorkbook.Sheets("Sheet1").Visible = True
    ThisWorkbook.Sheets("Sheet1").Select
    ThisWorkbook.Sheets("Sheet1").Range("a1").Select

ElseIf ComboBox1.Text = "2" Then
    ThisWorkbook.Sheets("Sheet2").Visible = True
    ThisWorkbook.Sheets("Sheet2").Select
    ThisWorkbook.Sheets("Sheet2").Range("a1").Select

ElseIf ComboBox1.Text = "3" Then
    ThisWorkbook.Sheets("Sheet3").Visible = True
    ThisWorkbook.Sheets("Sheet3").Select
    ThisWorkbook.Sheets("Sheet3").Range("a1").Select

End If