多个If语句缩进错误?

时间:2016-01-06 19:15:59

标签: vba if-statement indentation

所以,我认为我做错了什么但需要一些指导。

以下代码假设是基于组合框的单向计算"授予"结果。目前我的计算是错误地进行计算(因为它认为它是"两半都是相同的术语")。我相信这是由于我的缩进或不正确使用多个嵌套的if语句。

 If Awarding = "First Half All Credits in One Half" Or Awarding = "Second Half Only" Then
     If LEU.Value <> "" And PLEU < CalcElig Then
         Payment1.Value = PLEU
     If PAmtUnused < CalcElig Then
         Payment1.Value = PAmtUnused
     Else: Payment1.Value = Payment1.Value = Round(CLng(PSchAward) *      PTotalAyWeeks / PMinAyWeeks / 2, 0)
     End If

     PPayment1 = Payment1.Value
     PPayment2 = 0
     TotalPayment = PPayment1 + PPayment2
     End If
 End If


 If Awarding = "Both Halfs Same Term" Then
     If LEU.Value <> "" And PLEU < CalcEligA Then
         Payment1.Value = PLEU
     ElseIf PAmtUnused < CalcEligA Then
         Payment1.Value = PAmtUnused
     Else: Payment1.Value = CalcEligA
     End If


     PPayment1 = Payment1.Value


     If LEU.Value <> "" And PLEU > 0 Then
         Payment2.Value = PLEU - PPayment1
     Else: Payment2.Value = CalcEligB
     End If

     If PAmtUnused - PPayment1 < PLEU - PPayment1 Then
         Payment2.Value = PAmtUnused - PPayment1
     ElseIf PAmtUnused - PPayment1 < CalcEligB Then
         Payment2.Value = PAmtUnused - PPayment1
     Else: Payment2.Value = CalcEligB
     End If


     PPayment2 = Payment2.Value

     TotalPayment = PPayment1 + PPayment2

     End If

 End If

 End Sub

2 个答案:

答案 0 :(得分:2)

问题似乎出现在你的第一个街区。

您的代码(添加了缩进以显示流程):

If Awarding = "First Half All Credits in One Half" Or Awarding = "Second Half Only" Then
     If LEU.Value <> "" And PLEU < CalcElig Then
         Payment1.Value = PLEU
         If PAmtUnused < CalcElig Then
            'This will write over Payment1.Value if both If conditions are satisfied.
             Payment1.Value = PAmtUnused
         Else: Payment1.Value = Payment1.Value = Round(CLng(PSchAward) *      PTotalAyWeeks / PMinAyWeeks / 2, 0)
      'The else condition will write over Payment1.Value if the second If condition is not satisfied.
         End If
'Payment1.Value is NEVER set if the first IF statement evaluates to FALSE,
' and it is written over if the first IF statement evaluates to TRUE!!
         PPayment1 = Payment1.Value
         PPayment2 = 0
         TotalPayment = PPayment1 + PPayment2
     End If
End If

你可能意味着什么(由于其他方块):

If Awarding = "First Half All Credits in One Half" Or Awarding = "Second Half Only" Then
    If LEU.Value <> "" And PLEU < CalcElig Then
        Payment1.Value = PLEU
    ElseIf PAmtUnused < CalcElig Then
        Payment1.Value = PAmtUnused
    Else: Payment1.Value = Payment1.Value = Round(CLng(PSchAward) *      PTotalAyWeeks / PMinAyWeeks / 2, 0)
    End If

    PPayment1 = Payment1.Value
    PPayment2 = 0
    TotalPayment = PPayment1 + PPayment2

End If

还有一个问题。

这行代码:

Else: Payment1.Value = Payment1.Value = Round(CLng(PSchAward) *      PTotalAyWeeks / PMinAyWeeks / 2, 0)

将在Payment1中置为TRUE或FALSE。它与:

相同
 Else: Payment1.Value = (Payment1.Value = Round(CLng(PSchAward) *      PTotalAyWeeks / PMinAyWeeks / 2, 0))

其中括号中的部分是否等于等号两边的两个值是否相等。

你可能只想拥有一个平等:

Else: Payment1.Value = Round(CLng(PSchAward) *      PTotalAyWeeks / PMinAyWeeks / 2, 0)

找出这样的错误的最好方法是逐步完成代码,看看究竟发生了什么以及程序如何通过代码运行与您期望的相比。

答案 1 :(得分:0)

即使OpiesDad是一个很好的帮助,但事实证明有一些非常糟糕的语法问题。我不得不重新调整整个事情,使其更加混乱,更有意义。解决方案与原始帖子的INF不同,但我想发布回复。

主要争论点:

Trying to separate calculations 
Multiple static value declarations
Being dumb