多个 VBA if 语句触发不同的事件

multiple VBA if statements triggering different event

抱歉,我认为这是非常基本的,但我想知道是否有人可以告诉我为什么这些 IF 语句中只有 1 个似乎 运行。 IF "CASH" 选项的第 3 个 IF 语句有效,但不幸的是其他 2 个无效。

Sub HideUnhide_Discount()

If Range("Payment_Option") = "Subscription" Then
    Range("MnthD_Row").EntireRow.Hidden = False
    Range("MnthD").Value = 0
Else
    Range("MnthD_Row").EntireRow.Hidden = True
End If

If Range("Payment_Option") = "Lease" Then
    Range("OOD_Row").EntireRow.Hidden = False
    Range("Leasing_Info").EntireRow.Hidden = False
    Range("OOD").Value = 0
Else
    Range("OOD_Row").EntireRow.Hidden = True
    Range("Leasing_Info").EntireRow.Hidden = True
End If

If Range("Payment_Option") = "Cash" Then
    Range("OOD_Row").EntireRow.Hidden = False
    Range("MnthD_Row").EntireRow.Hidden = False
    Range("OOD").Value = 0
Else
    Range("OOD_Row").EntireRow.Hidden = True
    Range("MnthD_Row").EntireRow.Hidden = True
End If

End Sub

尝试用下面的 Select Case 替换您的多个 If >> Else 条件:

Sub HideUnhide_Discount()

' first reset all rows to be visible , later according to the value, unhide specific rows
Range("MnthD_Row").EntireRow.Hidden = True
Range("OOD_Row").EntireRow.Hidden = True
Range("Leasing_Info").EntireRow.Hidden = True

Select Case Range("Payment_Option")
    Case "Subscription"
        Range("MnthD_Row").EntireRow.Hidden = False
        Range("MnthD").Value = 0

    Case "Lease"
        Range("OOD_Row").EntireRow.Hidden = False
        Range("Leasing_Info").EntireRow.Hidden = False
        Range("OOD").Value = 0

    Case "Cash"
        Range("OOD_Row").EntireRow.Hidden = False
        Range("MnthD_Row").EntireRow.Hidden = False
        Range("OOD").Value = 0

End Select

End Sub