嵌套 If - 可能更好的解决方案?

Nested If - Possible better solution?

我希望就 VBA 中大量嵌套 If 语句的更好解决方案获得一些建议。由于可能的条件数量,If 语句变得非常大。我在考虑使用 For 循环,但这似乎仍然需要大量的 If 条件,我也想到了 Select Case,但这也不起作用.

下面的代码显示了我如何为 If 语句和大多数条件设置变量。如果能帮助简化此代码,我们将不胜感激。

    If wsCalc.Range("LenderComplete") <> vbNullString Then f = 1
If wsCalc.Range("ProcessorComplete") <> vbNullString Then g = 1
If wsCalc.Range("KeyerComplete") <> vbNullString Then h = 1
If wsCalc.Range("CheckerComplete") <> vbNullString Then i = 1

If f <> 1 And g <> 1 And h <> 1 And i <> 1 Then
    ContLoanFile.ContinueLP.BackColor = vbYellow
    With ContLoanFile.Label7
        .Caption = "Lender items NOT complete." & vbNewLine & "Processor items NOT complete." & vbNewLine & "Keyer items NOT complete." & vbNewLine & "Checker items NOT complete."
        .Font.Size = 9
    End With
    If f = 1 And g <> 1 And h <> 1 And i <> 1 Then
        ContLoanFile.ContinueLP.BackColor = vbYellow
        With ContLoanFile.Label7
            .Caption = "Lender items COMPLETE." & vbNewLine & "Processor items COMPLETE." & vbNewLine & "Keyer items NOT complete." & vbNewLine & "Checker items NOT complete."
            .Font.Size = 9
        End With
        If f = 1 And g = 1 And h <> 1 And i <> 1 Then
            ContLoanFile.ContinueLP.BackColor = vbYellow
            With ContLoanFile.Label7
                .Caption = "Lender items COMPLETE." & vbNewLine & "Processor items COMPLETE." & vbNewLine & "Keyer items NOT complete." & vbNewLine & "Checker items NOT complete."
                .Font.Size = 9
            End With
            If f = 1 And g = 1 And h = 1 And i <> 1 Then
                ContLoanFile.ContinueLP.BackColor = vbYellow
                With ContLoanFile.Label7
                    .Caption = "Lender items COMPLETE." & vbNewLine & "Processor items COMPLETE." & vbNewLine & "Keyer items COMPLETE." & vbNewLine & "Checker items NOT complete."
                    .Font.Size = 9
                End With
                If f = 1 And g = 1 And h = 1 And i = 1 Then
                    ContLoanFile.ContinueLP.BackColor = vbYellow
                    With ContLoanFile.Label7
                        .Caption = "Lender items COMPLETE." & vbNewLine & "Processor items COMPLETE." & vbNewLine & "Keyer items COMPLETE." & vbNewLine & "Checker items COMPLETE."
                        .Font.Size = 9
                    End With
                    If f <> 1 And g = 1 And h = 1 And i = 1 Then
                        ContLoanFile.ContinueLP.BackColor = vbYellow
                        With ContLoanFile.Label7
                            .Caption = "Lender items NOT complete." & vbNewLine & "Processor items COMPLETE." & vbNewLine & "Keyer items COMPLETE." & vbNewLine & "Checker items COMPLETE."
                            .Font.Size = 9
                        End With
                        If f = 1 And g <> 1 And h = 1 And i = 1 Then
                            ContLoanFile.ContinueLP.BackColor = vbYellow
                            With ContLoanFile.Label7
                                .Caption = "Lender items COMPLETE." & vbNewLine & "Processor items NOT complete." & vbNewLine & "Keyer items COMPLETE." & vbNewLine & "Checker items COMPLETE."
                                .Font.Size = 9
                            End With

首先,它们不需要嵌套,因为它们都有相互冲突的逻辑,永远不会 "cascade" 像嵌套 If 那样。

If f <> 1 And g <> 1 And h <> 1 And i <> 1 Then '// If this is true
    '...
    If f = 1 And g <> 1 And h <> 1 And i <> 1 Then '// Then this can never be true

如果您要保留此设计,那么它确实应该 If...ElseIf... 符合逻辑。


其次,你有很多共同的逻辑,所以这样的事情应该有效,因为你不必为每个条件重复相同的结果:

Dim caption As String

caption = "Lender items " & IIf(wsCalc.Range("LenderComplete").Value <> vbNullString, "COMPLETE", "NOT complete") & vbNewLine & _
          "Processor items " & IIf(wsCalc.Range("ProcessorComplete").Value <> vbNullString, "COMPLETE", "NOT complete") & vbNewLine & _
          "Keyer items " & IIf(wsCalc.Range("KeyerComplete").Value <> vbNullString, "COMPLETE", "NOT complete") & vbNewLine & _
          "Checker items " & IIf(wsCalc.Range("CheckerComplete").Value <> vbNullString, "COMPLETE", "NOT complete")


With ContLoanFile
    .ContinueLP.BackColor = vbYellow
    With .Label7
        .Caption = caption
        .Font.Size = 9
    End With
End With

不需要循环,也不需要多次测试相同的值。