多个 If 语句缩进错误?
Multiple If Statements Indentation Error?
所以,我认为我做错了什么,但需要一点指导。
下面的代码应该是一种基于组合框 "Awarding" 结果的计算方式。目前我的计算不正确(因为它认为它是 "both halfs same term")。我认为这是由于我的缩进或错误使用了多个嵌套 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
问题似乎出在您的第一个区块中。
您的代码(添加缩进以显示流程):
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)
找出此类错误的最佳方法是单步执行代码,查看到底发生了什么,以及程序 运行 通过代码与您预期的相比如何。
尽管 OpiesDad 提供了很大的帮助,但事实证明存在一些非常糟糕的语法问题。我不得不重新设计整个东西,以减少混乱并使之更有意义。该解决方案与原始 post 不同,但我想对 post 做出回应。
争论要点:
Trying to separate calculations
Multiple static value declarations
Being dumb
所以,我认为我做错了什么,但需要一点指导。
下面的代码应该是一种基于组合框 "Awarding" 结果的计算方式。目前我的计算不正确(因为它认为它是 "both halfs same term")。我认为这是由于我的缩进或错误使用了多个嵌套 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
问题似乎出在您的第一个区块中。
您的代码(添加缩进以显示流程):
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)
找出此类错误的最佳方法是单步执行代码,查看到底发生了什么,以及程序 运行 通过代码与您预期的相比如何。
尽管 OpiesDad 提供了很大的帮助,但事实证明存在一些非常糟糕的语法问题。我不得不重新设计整个东西,以减少混乱并使之更有意义。该解决方案与原始 post 不同,但我想对 post 做出回应。
争论要点:
Trying to separate calculations
Multiple static value declarations
Being dumb