第一次使用 Case 语句出错
1st Time Using Case Statement Getting Errors
下面是我尝试在多页用户窗体命令按钮上使用的代码。此按钮应首先检查您是否选择了 7 个选项按钮之一。如果你还没有,它应该要求你 "Select Site"。如果您这样做了,那么根据您选择的选项,它应该为一大组命名文本框生成默认值。但是,当代码运行时,我收到一条错误消息,指出 "Statements or labels invalid between Select Case and first Case"。我不熟悉在 VBA 中使用 Case 语句,但我认为它可能比嵌套的 ifs 更好用。但是看来我做错了什么,希望你们能帮我弄清楚是什么。
我正在使用 Excel 2013,但是文件将在使用 Excel 2007 的机器上 运行。
Private Sub SiteNext_Click()
If Me.OptionButton1.Value = False And Me.OptionButton2.Value = False And Me.OptionButton3.Value = False And Me.OptionButton4.Value = False And _
Me.OptionButton5.Value = False And Me.OptionButton6.Value = False And Me.OptionButton7.Value = False Then
MsgBox "Select Site"
Exit Sub
Else
Select Case Me.OptionButton1.Value = True
LocalTier1.Value = "600"
LocalTier2.Value = "250"
LocalTier3.Value = "175"
LocalTier4.Value = "125"
LocalTier5.Value = "85"
LocalTier6.Value = "65"
LocalTier7.Value = "40"
LocalTier8.Value = "25"
LocalTier9.Value = "10"
LocalTier10.Value = "5"
LocalTier11.Value = "5"
DriveTier1.Value = "600"
DriveTier2.Value = "250"
DriveTier3.Value = "175"
DriveTier4.Value = "125"
DriveTier5.Value = "85"
DriveTier6.Value = "65"
DriveTier7.Value = "40"
DriveTier8.Value = "25"
DriveTier9.Value = "10"
DriveTier10.Value = "5"
DriveTier11.Value = "5"
OuterTier1.Value = "600"
OuterTier2.Value = "250"
OuterTier3.Value = "200"
OuterTier4.Value = "150"
OuterTier5.Value = "100"
OuterTier6.Value = "75"
OuterTier7.Value = "45"
OuterTier8.Value = "30"
OuterTier9.Value = "15"
OuterTier10.Value = "10"
OuterTier11.Value = "5"
Case Me.OptionButton2.Value = True
LocalTier1.Value = "250"
LocalTier2.Value = "225"
LocalTier3.Value = "150"
LocalTier4.Value = "120"
LocalTier5.Value = "80"
LocalTier6.Value = "50"
LocalTier7.Value = "25"
LocalTier8.Value = "15"
LocalTier9.Value = "10"
LocalTier10.Value = "5"
LocalTier11.Value = "5"
DriveTier1.Value = "300"
DriveTier2.Value = "225"
DriveTier3.Value = "150"
DriveTier4.Value = "120"
DriveTier5.Value = "80"
DriveTier6.Value = "50"
DriveTier7.Value = "25"
DriveTier8.Value = "15"
DriveTier9.Value = "10"
DriveTier10.Value = "5"
DriveTier11.Value = "5"
Case Me.OptionButton3.Value = True
LocalTier1.Value = "200"
LocalTier2.Value = "175"
LocalTier3.Value = "125"
LocalTier4.Value = "75"
LocalTier5.Value = "60"
LocalTier6.Value = "40"
LocalTier7.Value = "20"
LocalTier8.Value = "15"
LocalTier9.Value = "10"
LocalTier10.Value = "5"
LocalTier11.Value = "5"
DriveTier1.Value = "225"
DriveTier2.Value = "200"
DriveTier3.Value = "150"
DriveTier4.Value = "100"
DriveTier5.Value = "80"
DriveTier6.Value = "60"
DriveTier7.Value = "30"
DriveTier8.Value = "20"
DriveTier9.Value = "15"
DriveTier10.Value = "10"
DriveTier11.Value = "10"
Case Me.OptionButton4.Value = True
LocalTier1.Value = "175"
LocalTier2.Value = "150"
LocalTier3.Value = "100"
LocalTier4.Value = "60"
LocalTier5.Value = "45"
LocalTier6.Value = "25"
LocalTier7.Value = "10"
LocalTier8.Value = "5"
LocalTier9.Value = "5"
DriveTier1.Value = "200"
DriveTier2.Value = "175"
DriveTier3.Value = "125"
DriveTier4.Value = "100"
DriveTier5.Value = "50"
DriveTier6.Value = "30"
DriveTier7.Value = "15"
DriveTier8.Value = "10"
DriveTier9.Value = "10"
Case Me.OptionButton5.Value = True
LocalTier1.Value = "175"
LocalTier2.Value = "150"
LocalTier3.Value = "100"
LocalTier4.Value = "60"
LocalTier5.Value = "40"
LocalTier6.Value = "20"
LocalTier7.Value = "15"
LocalTier8.Value = "10"
LocalTier9.Value = "5"
LocalTier10.Value = "5"
DriveTier1.Value = "175"
DriveTier2.Value = "150"
DriveTier3.Value = "100"
DriveTier4.Value = "60"
DriveTier5.Value = "40"
DriveTier6.Value = "20"
DriveTier7.Value = "15"
DriveTier8.Value = "10"
DriveTier9.Value = "5"
DriveTier10.Value = "5"
Case Me.OptionButton6.Value = True
LocalTier1.Value = "75"
LocalTier2.Value = "50"
LocalTier3.Value = "30"
LocalTier4.Value = "20"
LocalTier5.Value = "15"
LocalTier6.Value = "10"
LocalTier7.Value = "5"
DriveTier1.Value = "75"
DriveTier2.Value = "50"
DriveTier3.Value = "30"
DriveTier4.Value = "20"
DriveTier5.Value = "15"
DriveTier6.Value = "10"
DriveTier7.Value = "5"
Case Me.OptionButton7.Value = True
LocalTier1.Value = "50"
LocalTier2.Value = "25"
LocalTier3.Value = "20"
LocalTier4.Value = "10"
LocalTier5.Value = "5"
LocalTier6.Value = "5"
DriveTier1.Value = "50"
DriveTier2.Value = "25"
DriveTier3.Value = "20"
DriveTier4.Value = "10"
DriveTier5.Value = "5"
DriveTier6.Value = "5"
End Select
Me.MultiPage1.Value = 1
End If
End Sub
您可以使用通用 Select Case True
。它将评估每个案例并在第一个真实案例中执行操作:
Select Case True
Case Me.OptionButton1.Value = True
' actions ...
Case Me.OptionButton2.Value = True
' actions ...
End Select
下面是我尝试在多页用户窗体命令按钮上使用的代码。此按钮应首先检查您是否选择了 7 个选项按钮之一。如果你还没有,它应该要求你 "Select Site"。如果您这样做了,那么根据您选择的选项,它应该为一大组命名文本框生成默认值。但是,当代码运行时,我收到一条错误消息,指出 "Statements or labels invalid between Select Case and first Case"。我不熟悉在 VBA 中使用 Case 语句,但我认为它可能比嵌套的 ifs 更好用。但是看来我做错了什么,希望你们能帮我弄清楚是什么。
我正在使用 Excel 2013,但是文件将在使用 Excel 2007 的机器上 运行。
Private Sub SiteNext_Click()
If Me.OptionButton1.Value = False And Me.OptionButton2.Value = False And Me.OptionButton3.Value = False And Me.OptionButton4.Value = False And _
Me.OptionButton5.Value = False And Me.OptionButton6.Value = False And Me.OptionButton7.Value = False Then
MsgBox "Select Site"
Exit Sub
Else
Select Case Me.OptionButton1.Value = True
LocalTier1.Value = "600"
LocalTier2.Value = "250"
LocalTier3.Value = "175"
LocalTier4.Value = "125"
LocalTier5.Value = "85"
LocalTier6.Value = "65"
LocalTier7.Value = "40"
LocalTier8.Value = "25"
LocalTier9.Value = "10"
LocalTier10.Value = "5"
LocalTier11.Value = "5"
DriveTier1.Value = "600"
DriveTier2.Value = "250"
DriveTier3.Value = "175"
DriveTier4.Value = "125"
DriveTier5.Value = "85"
DriveTier6.Value = "65"
DriveTier7.Value = "40"
DriveTier8.Value = "25"
DriveTier9.Value = "10"
DriveTier10.Value = "5"
DriveTier11.Value = "5"
OuterTier1.Value = "600"
OuterTier2.Value = "250"
OuterTier3.Value = "200"
OuterTier4.Value = "150"
OuterTier5.Value = "100"
OuterTier6.Value = "75"
OuterTier7.Value = "45"
OuterTier8.Value = "30"
OuterTier9.Value = "15"
OuterTier10.Value = "10"
OuterTier11.Value = "5"
Case Me.OptionButton2.Value = True
LocalTier1.Value = "250"
LocalTier2.Value = "225"
LocalTier3.Value = "150"
LocalTier4.Value = "120"
LocalTier5.Value = "80"
LocalTier6.Value = "50"
LocalTier7.Value = "25"
LocalTier8.Value = "15"
LocalTier9.Value = "10"
LocalTier10.Value = "5"
LocalTier11.Value = "5"
DriveTier1.Value = "300"
DriveTier2.Value = "225"
DriveTier3.Value = "150"
DriveTier4.Value = "120"
DriveTier5.Value = "80"
DriveTier6.Value = "50"
DriveTier7.Value = "25"
DriveTier8.Value = "15"
DriveTier9.Value = "10"
DriveTier10.Value = "5"
DriveTier11.Value = "5"
Case Me.OptionButton3.Value = True
LocalTier1.Value = "200"
LocalTier2.Value = "175"
LocalTier3.Value = "125"
LocalTier4.Value = "75"
LocalTier5.Value = "60"
LocalTier6.Value = "40"
LocalTier7.Value = "20"
LocalTier8.Value = "15"
LocalTier9.Value = "10"
LocalTier10.Value = "5"
LocalTier11.Value = "5"
DriveTier1.Value = "225"
DriveTier2.Value = "200"
DriveTier3.Value = "150"
DriveTier4.Value = "100"
DriveTier5.Value = "80"
DriveTier6.Value = "60"
DriveTier7.Value = "30"
DriveTier8.Value = "20"
DriveTier9.Value = "15"
DriveTier10.Value = "10"
DriveTier11.Value = "10"
Case Me.OptionButton4.Value = True
LocalTier1.Value = "175"
LocalTier2.Value = "150"
LocalTier3.Value = "100"
LocalTier4.Value = "60"
LocalTier5.Value = "45"
LocalTier6.Value = "25"
LocalTier7.Value = "10"
LocalTier8.Value = "5"
LocalTier9.Value = "5"
DriveTier1.Value = "200"
DriveTier2.Value = "175"
DriveTier3.Value = "125"
DriveTier4.Value = "100"
DriveTier5.Value = "50"
DriveTier6.Value = "30"
DriveTier7.Value = "15"
DriveTier8.Value = "10"
DriveTier9.Value = "10"
Case Me.OptionButton5.Value = True
LocalTier1.Value = "175"
LocalTier2.Value = "150"
LocalTier3.Value = "100"
LocalTier4.Value = "60"
LocalTier5.Value = "40"
LocalTier6.Value = "20"
LocalTier7.Value = "15"
LocalTier8.Value = "10"
LocalTier9.Value = "5"
LocalTier10.Value = "5"
DriveTier1.Value = "175"
DriveTier2.Value = "150"
DriveTier3.Value = "100"
DriveTier4.Value = "60"
DriveTier5.Value = "40"
DriveTier6.Value = "20"
DriveTier7.Value = "15"
DriveTier8.Value = "10"
DriveTier9.Value = "5"
DriveTier10.Value = "5"
Case Me.OptionButton6.Value = True
LocalTier1.Value = "75"
LocalTier2.Value = "50"
LocalTier3.Value = "30"
LocalTier4.Value = "20"
LocalTier5.Value = "15"
LocalTier6.Value = "10"
LocalTier7.Value = "5"
DriveTier1.Value = "75"
DriveTier2.Value = "50"
DriveTier3.Value = "30"
DriveTier4.Value = "20"
DriveTier5.Value = "15"
DriveTier6.Value = "10"
DriveTier7.Value = "5"
Case Me.OptionButton7.Value = True
LocalTier1.Value = "50"
LocalTier2.Value = "25"
LocalTier3.Value = "20"
LocalTier4.Value = "10"
LocalTier5.Value = "5"
LocalTier6.Value = "5"
DriveTier1.Value = "50"
DriveTier2.Value = "25"
DriveTier3.Value = "20"
DriveTier4.Value = "10"
DriveTier5.Value = "5"
DriveTier6.Value = "5"
End Select
Me.MultiPage1.Value = 1
End If
End Sub
您可以使用通用 Select Case True
。它将评估每个案例并在第一个真实案例中执行操作:
Select Case True
Case Me.OptionButton1.Value = True
' actions ...
Case Me.OptionButton2.Value = True
' actions ...
End Select