【问题标题】:If with multiple ands, nested ifs, or ifs with booleans?如果有多个ands,嵌套ifs,还是带有布尔值的ifs?
【发布时间】:2020-06-03 21:13:01
【问题描述】:

我已经尝试了多种方法,但仍然没有得到我需要的结果。我有一个带有多个复选框的用户窗体。如果选中了两个复选框,我想要一个答案,但如果选中第三个复选框,则需要一个单独的答案。这样做的正确方法是什么? 嵌套 Ifs:

Dim Cost As Range, SetUpCost As Range, Prices As Range
Set Cost = WB.Worksheets("Calculations").Range("F42")
Set Prices = WB.Worksheets("Calculations").Range("D42")

If QuarterlyCheck = True And SecurityLite = True Then
If NoSetUpCheck = False Then
    SecurityLitePrice.Caption = "$" & Cost.Value * 4
    SetUpFee.Caption = "$" & SetUpCost.Value
End If
ElseIf NoSetUpCheck = True Then
    SecurityLitePrice.Caption = "$" & Prices.Value * 4
    SetUpFee.Caption = ""
End If

或多个 if:

If QuarterlyCheck = True And SecurityLite = True And NoSetUpCheck = False Then
    SecurityLitePrice.Caption = "$" & Cost.Value * 4
    SetUpFee.Caption = "$" & SetUpCost.Value
End If
If QuarterlyCheck = True And SecurityLite = True And NoSetUpCheck = True Then
    SecurityLitePrice.Caption = "$" & Prices.Value * 4
    SetUpFee.Caption = ""
End If

两者都没有得到我需要的结果。

【问题讨论】:

  • 如果选中第三个复选框,是否会取代同时选中复选框 1 和 2?那么1 = True and 2 = True1 = False and 2 = False 是否与3 = true 无关?
  • 不,必须选中复选框 1 和 2 以与用户窗体上的其他相关子项一致。选中 3(或取消选中)会改变结果。

标签: vba if-statement boolean


【解决方案1】:

也许:

If QuarterlyCheck = True And SecurityLite = True Then
    If NoSetUpCheck = True Then
        SecurityLitePrice.Caption = "$" & Prices.Value * 4
        SetUpFee.Caption = ""
    Else 'Means NoSetUpCheck = False
        SecurityLitePrice.Caption = "$" & Cost.Value * 4
        SetUpFee.Caption = "$" & SetUpCost.Value
    End If
Else
    'Whatever else ....
End If

【讨论】:

  • 从技术上讲,它应该可以满足您的要求。究竟是什么不起作用?还有什么我们需要知道的吗?单元格 D42 和 F42 中是否有任何值?
  • 是的,我查过了。也许我需要考虑重组表单。
  • 您应该在您的用户表单中有一个触发器来激活该代码? button_OK_Click()optionSomething_Change_Click() 之类的东西……
猜你喜欢
  • 1970-01-01
  • 2013-11-10
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 2019-01-30
相关资源
最近更新 更多