"选择Case True" - 添加更多标准

时间:2017-12-08 09:25:04

标签: vba

我添加了这个新位后,下面的代码停止了工作:

Select Case True
    Case Range("B16") = "High Risk"
        Rows("5:12").EntireRow.Hidden = False
        Rows("24").EntireRow.Hidden = False
    Case Range("B16") = "Medium Risk"
        Rows("5:12").EntireRow.Hidden = True
        Rows("24").EntireRow.Hidden = True
    Case Range("B16") = "Standard Risk"
        Rows("5:12").EntireRow.Hidden = True
        Rows("24").EntireRow.Hidden = True
    End Select

完整代码:

Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Application.ScreenUpdating = False

    Select Case True
    Case Range("B14") = "Medium Risk"
        Rows("5:12").EntireRow.Hidden = True
        Rows("24").EntireRow.Hidden = True
    Case Range("B14") = "High Risk"
        Rows("5:12").EntireRow.Hidden = False
        Rows("24").EntireRow.Hidden = False
    End Select

Select Case True
    Case Range("XES72") = "Medium Risk"
        Rows("5:12").EntireRow.Hidden = True
        Rows("24").EntireRow.Hidden = True
    Case Range("XES72") = "High Risk"
        Rows("5:12").EntireRow.Hidden = False
        Rows("24").EntireRow.Hidden = False
    End Select

Select Case True
    Case Range("B16") = "High Risk"
        Rows("5:12").EntireRow.Hidden = False
        Rows("24").EntireRow.Hidden = False
    Case Range("B16") = "Medium Risk"
        Rows("5:12").EntireRow.Hidden = True
        Rows("24").EntireRow.Hidden = True
    Case Range("B16") = "Standard Risk"
        Rows("5:12").EntireRow.Hidden = True
        Rows("24").EntireRow.Hidden = True
    End Select

Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

任何建议如何确保"新"我添加的部分也被VBA考虑过了?

有效的部分就是这个(及其上面的一切):

Select Case True
    Case Range("B16") = "High Risk"
        Rows("5:12").EntireRow.Hidden = False
        Rows("24").EntireRow.Hidden = False

然后,如果我将B16改回"中等风险" (或"标准风险")它根本不隐藏它应该的行:

 Case Range("B16") = "Medium Risk"
            Rows("5:12").EntireRow.Hidden = True
            Rows("24").EntireRow.Hidden = True
 Case Range("B16") = "Standard Risk"
            Rows("5:12").EntireRow.Hidden = True
            Rows("24").EntireRow.Hidden = True
        End Select

1 个答案:

答案 0 :(得分:4)

你的Select Case statements应该有点不同。而不是这个逻辑:

Select Case True
    Case Range("B16") = "High Risk"
    ...
    ...

你应该使用这个逻辑:

Select Case Range("B16")  '<< check the value of cell B16 ...
    Case "High Risk"      '<< ... in case it equals 'High Risk'...
    ...                   '<< ... do this
    ...