所以,我认为我做错了什么,但需要一些指导。
下面的代码应该是基于组合框"奖励"结果的一种计算方式。目前,我的计算是错误的(因为它认为这是"两个半相同的术语")。我相信这是由于我的缩进或不正确地使用了多个嵌套的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是一个很好的帮助,但事实证明存在一些非常糟糕的语法问题。我不得不重新调整整个东西,使其不那么杂乱,更有意义。解决方案是INF不同于原来的帖子,但我想发布一个回应。
主要争论点:
Trying to separate calculations
Multiple static value declarations
Being dumb