是否嵌套-可能有更好的解决方案?

时间:2018-10-09 20:55:03

标签: vba excel-2013

我希望获得有关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

1 个答案:

答案 0 :(得分:3)

首先,它们不需要嵌套,因为它们都具有相互冲突的逻辑,并且永远不会像嵌套的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

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