Select Case vba中的多个语句

时间:2015-08-07 08:34:18

标签: excel vba excel-vba

我想在excel vba中使用Select Case选项来过滤正确的数据。 我做了以下事情:

For i = 2 To LastRow  

    StartTime = Worksheets("Task input").Cells(1 + i, 9)
    Status = Worksheets("Task input").Cells(1 + i, 14)

    Select Case StartTime And Status

           Case TimeWindowBound1 To TimeWindowBound2 And "Planned"
                Worksheets("Performance output").Cells(5, 2) = Worksheets("Performance output").Cells(5, 2) + 1

    End Select

Next i

他比较StartTime和Status。但是这会出错?任何解决方案?

2 个答案:

答案 0 :(得分:3)

您不能直接在选择案例

中执行此操作

(选择案例https://msdn.microsoft.com/en-us/library/cy37t14y.aspx的文档)

select case只能用于单个变量。 但这里的解决方案并不复杂:

Select Case StartTime
Case TimeWindowBound1 To TimeWindowBound2
    If Status = "Planned" then Worksheets("Performance output").Cells(5, 2) = Worksheets("Performance output").Cells(5, 2) + 1
End Select

答案 1 :(得分:0)

真的,我对你的代码感到惊讶。我还没有找到这样的东西。

您需要知道的一件事是SELECT CASE [condition] AND [condition]无法在任何地方使用。

但你需要检查两者,所以请尝试以下方法:

Public Sub testSelect()

    Dim sDate As Date
    Dim sString As String

    'Set test date
    sDate = "10/02/2014"

    'Set test string
    sString = "select"

    'First select date, because it need to check in a range
    Select Case sDate

        'If it is in desire range, check string
        Case "10/01/2014" To "10/30/2014"

            'If string is equal, show message for True.
            If sString = "select" Then
                MsgBox ("True")

            'Else show message for False
            Else
                MsgBox ("False")
            End If

    End Select

End Sub