字符串案例的不选择案例的案例

Posted

技术标签:

【中文标题】字符串案例的不选择案例的案例【英文标题】:Case Without Select Case for String Cases 【发布时间】:2018-05-04 17:59:52 【问题描述】:

我正在尝试为每个可以传入的不同字符串组合选择不同的大小写。在这种情况下,我使用棒球位置的缩写。

为了方便起见,我添加了一个名为“Position”的字符串值作为比较对象。

我目前收到错误:

编译错误:没有选择案例的案例

我认为这是某种格式错误,但我无法在网上找到任何可以修复它的东西。

Position = "test"
        Select Case Position
            Case "C"
                If C.DollarPerWar < Cells(i, 8).Value Then
                    Set C = AssignPlayer(C, CurrentPlayer)
            Case "1B"
                If B1.DollarPerWar < Cells(i, 8).Value Then
                    Set B1 = AssignPlayer(B1, CurrentPlayer)
            Case "2B"
                If B2.DollarPerWar < Cells(i, 8).Value Then
                    Set B2 = AssignPlayer(B2, CurrentPlayer)
            Case "3B"
                If B3.DollarPerWar < Cells(i, 8).Value Then
                    Set B3 = AssignPlayer(B3, CurrentPlayer)
            Case "SS"
                If SS.DollarPerWar < Cells(i, 8).Value Then
                    Set SS = AssignPlayer(SS, CurrentPlayer)
            Case "LF"
                If LF.DollarPerWar < Cells(i, 8).Value Then
                    Set LF = AssignPlayer(LF, CurrentPlayer)
            Case "CF"
                If CF.DollarPerWar < Cells(i, 8).Value Then
                    Set CF = AssignPlayer(CF, CurrentPlayer)
            Case "RF"
                If RF.DollarPerWar < Cells(i, 8).Value Then
                    Set RF = AssignPlayer(RF, CurrentPlayer)
        End Select

任何帮助将不胜感激!

【问题讨论】:

您的If 都没有End If。要么将End If 添加到每个语句中,要么将整个if 语句放在一行中。 正如@VictorK 提到的,将 End if 添加到您的 IF 或使用此`IF`条件THEN: 语句。 FWIW 使用indenter (注​​意:在线演示需要一个实际的范围,即你需要Sub...End Sub 才能工作)会使问题变得相当明显;-)跨度> 【参考方案1】:

在你的 if 语句中终止你的新行。它们应该是一行,如下所示:

If RF.DollarPerWar < Cells(i, 8).Value Then Set RF = AssignPlayer(RF, CurrentPlayer)

【讨论】:

或者,将End If 添加到下面的行中。 是的,但除非有其他要求,否则不需要这样做。少即是美? 如果你不打算使用End If,我认为使用续行符_ 而不是全部写成一行会更容易阅读。

以上是关于字符串案例的不选择案例的案例的主要内容,如果未能解决你的问题,请参考以下文章

VBA案例选择多个条件

选择具有复杂返回值的案例 (VBA 2007)

使用列表和数组进行切换和大小写

选择案例的 Python 等效项 (CDec(variable)

MS Access VBA SQL查询调试选择案例

用一个案例演示数值或字符串类型字段的选择度对执行计划的影响