2016-01-06 69 views
-1

所以,我認爲我做錯了什麼,但需要一點指導。多個If語句縮進錯誤?

下面的代碼假設是基於組合框「頒獎」結果的一種計算方法。目前我的計算是錯誤地進行計算(因爲它認爲它是「兩個半數相同的期限」)。我相信這是由於我的縮進或不正確使用多個嵌套的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

如果你已經用Python編程,那麼你可能已經內化了縮進很重要的概念。原則上,在VBA中並不重要 - 儘管換行符形式的空白作爲語句終結者很重要。儘管如此,一致的縮進是一個非常好的主意,即使沒有人可以讀取代碼,因爲除此之外,它是調試中的一個主要幫助。 –

回答

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) 

將會把TRUE或FALSE的Payment1。它是一樣的:

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

其中括號的部分被評價爲對平等的任一側上的兩個值是否登錄相等。

你大概意思只有一個等式:

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

最好的辦法找出這樣的錯誤,這是單步執行代碼,看看究竟發生了怎樣的程序是通過運行代碼相比你期望的。

+0

感謝您指出所有的語法錯誤。主要問題仍然是,如果出現「如果獲獎=」兩個Halfs同期「」代碼的末尾是用戶表單的最終輸出,那麼當我頒發「頒獎=」前半部分所有學分的一半「或頒獎= 「僅限於下半場」「 –

+1

這是什麼意思? 「用戶表單的最終輸出」?哪行代碼產生這個輸出?什麼行導致它在用戶表單上?你給了什麼投入(就變量頒獎而言),以及這個投入的結果究竟發生了什麼?在逐步完成代碼之後,程序如何執行? – OpiesDad

+1

此外,如果我提供的修復導致了相同的問題,那麼我會更新問題以包含具有固定語法的代碼,然後解釋問題的原因,包括對我在之前的評論中提出的問題的回答。 – OpiesDad

0

儘管OpiesDad的幫助很大,但事實證明有一些非常糟糕的語法問題。我不得不重新整理整個事情,以減少混亂和更有意義。解決方案是INF與原始帖子不同,但我想發佈回覆。爭論的

主要點:

Trying to separate calculations 
Multiple static value declarations 
Being dumb