选择案例<>有多个标准

时间:2015-09-03 16:58:54

标签: excel excel-vba case vba

我有一个案例陈述的代码,但它只适用于第一个参数(debug.prints sheet2和3,请有人协助

Sub test()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        Select Case ws.CodeName
            Case Is <> "Sheet1", "Sheet2"
                Debug.Print ws.CodeName
        End Select
    Next ws
End Sub

2 个答案:

答案 0 :(得分:1)

我无法解释为什么这不起作用,但这是一个解决方法。您可以将其作为一个空案例,然后在其他情况下执行您的操作。

Sub test()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    Select Case ws.CodeName
        Case "Sheet1" To "Sheet2"

        Case Else
        Debug.Print ws.CodeName
    End Select
Next ws
End Sub

或者你可以用if语句而不是case来测试。

If ws.CodeName <> "Sheet1" AND ws.CodeName <> "Sheet2" then
    Debug.Print ws.CodeName
End if

答案 1 :(得分:0)

这将有效:

Sub test()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        Select Case ws.CodeName
            Case Is <> "Sheet1", Is <> "Sheet2"
                Debug.Print ws.CodeName
        End Select
    Next ws
End Sub