2016-08-02 73 views
2

無法確定爲什麼下面的代碼在'假設'時不會退出。任何幫助讚賞。問題是父Do While迴路沒有儘快結束迴路RF達到0.Nest Do雖然VBA中的循環沒有結束

我是否設置完全錯誤?不太熟悉VBA,並且幾年以來沒有用任何語言編寫程序,所以我非常生疏。

預先感謝您的任何見解!


Private Sub CalculateButton_Click() 
'Assigning Variables 
Dim cid As Currency 
Dim tip As Currency 
Dim cha As Currency 
Dim A1 As Currency 
Dim R1 As Currency 
Dim B1 As Boolean 
Dim A5 As Currency 
Dim R5 As Currency 
Dim B5 As Boolean 
Dim A10 As Currency 
Dim R10 As Currency 
Dim B10 As Boolean 
Dim A20 As Currency 
Dim R20 As Currency 
Dim B20 As Boolean 
Dim A50 As Currency 
Dim R50 As Currency 
Dim A100 As Currency 
Dim R100 As Currency 
Dim B100 As Boolean 
Dim tipCalc As Double 
Dim RF As Currency  

'Setting Boolean as false 
B1 = False 
B5 = False 
B10 = False 
B20 = False 
B100 = False 

'Setting Variables values 
cid = 587.87 
tip = 16 
cha = 13 
A1 = 36 
R1 = 0 
A5 = 85 
R5 = 0 
A10 = 50 
R10 = 0 
A20 = 420 
R20 = 0 
A50 = 0 
R50 = 0 
A100 = 0 
R100 = 0 
RF = 175 
If A1 + A5 + A10 + A20 + A50 + A100 < RF Then 
    MsgBox "Not Enough Money In Register" 
    Exit Sub 
End If 


'Grabbing decimal from CID to calculate tip rounding next 
tipCalc = cid - Int(cid) 

'Calculating tip and entering into the excel sheet 
'Removed for simplification 

RF = RF - cha 

Do While RF > 0 
    Do While B1 = False 'This is for the 1 dollar bills 
     If CheckWhole(RF, 5) And A1 < 5 Then 
      B1 = True 
      MsgBox "1 dollar done " & RF 

     Else 
      RF = RF - 1 
      A1 = A1 - 1 
      R1 = R1 + 1 
     End If 
    Loop 
    MsgBox RF > 0 
    Do While B5 = False '5 dollar bills 
     If CheckWhole(RF, 10) And A5 < 10 Then 
      B5 = True 
      MsgBox "5 dollar done " & RF 
     Else 
      RF = RF - 5 
      A5 = A5 - 5 
      R5 = R5 + 5 
     End If 
    Loop 
    MsgBox RF > 0 
    Do While B10 = False '10 dollar bills 
     If CheckWhole(RF, 20) And A10 < 20 Then 
      B10 = True 
      MsgBox "10 dollar done " & RF 
     Else 
      RF = RF - 10 
      A10 = A10 - 10 
      R10 = R10 + 10 
     End If 
    Loop 
    MsgBox RF > 0 
    Do While B20 = False '20 dollar bills 
     If CheckWhole(RF, 100) And A20 < 100 Then 
      B20 = True 
      MsgBox "20 dollar done " & RF 
     Else 
      RF = RF - 20 
      A20 = A20 - 20 
      R20 = R20 + 20 
      MsgBox "20 dollar working " & RF 
     End If 
    Loop 
    MsgBox "a20 " & RF > 0 

Loop 
'Output goes here, inputs data into cells on spreadsheet. 



End Sub 

Function CheckWhole(x As Currency, y As Currency) As Boolean 
    If Int(x/y) = (x/y) Then 
     CheckWhole = True 
    Else 
     CheckWhole = False 
    End If 
End Function 

注意,我編輯了一些代碼,是工作/不手頭涉及到這個問題。

是的,我有很多變數。我知道可能有更簡單的方法來編寫我正在嘗試做的事情。首先是

+0

如果由於操作終止條件而導致外環的條件匹配,您應該檢查內循環 – lokusking

+0

代碼中有一個邏輯漏洞。當滿足內部環路中的所有if條件時,RF不會遞減,所以外部環路無限循環。 –

+0

不,這是不正確的,外環不是無限的。 – Mono

回答

2

埃裏克,如果我理解你的權利,你做希望執行就可以進入內循環,如果RF陽性。一個簡單的方法是將這個要求明確地添加到所有的內部循環中。第一個內環將例如看起來像這樣開頭,那麼:

Do While RF > 0 And B1 = False'This is for the 1 dollar bills 

如果你做同樣的編輯其他三個內環,因爲我覺得你就應該把它可能工作。 Regards, Mats

+0

我會盡力感謝!我去睡覺,意識到嵌套循環並不是最好的路線 – Eric

0

。外環將退出。但只有當它到達代碼中相應的「循環」點時纔可以。因此,RF將變爲負值,並且在外環檢查下一個「輪」的條件之前,內環將全部完成。

除了該行:

MsgBox "a20 " & RF > 0 

您的代碼可以編譯和運行,但也許並不像你期望的那樣。因此,不要問爲什麼你的代碼不工作,你應該也可以解釋你想要達到的目的。

+0

正試圖做到這一半睡着了,今天早上有一個更清晰的方法,但謝謝你。 – Eric

4

如果你想同時其處理你需要把在檢查之前每一個新Do While檢查RF等於或小於0停止循環:

if RF <= 0 then exit Do 
+0

謝謝,這將工作,但我意識到,嵌套循環不是真正的最好的辦法來解決這個問題,更清醒後去另一條路線! – Eric

2

DragonSamu是正確的;你應該在每個內部循環前添加if RF <= 0 then exit Do

  • 做,當:如果條件滿足
  • 循環,直到將啓動循環:將重啓循環,直到條件滿足

在這裏,我創建了一個Function TransferFunds處理內部循環。 TransferFunds中的參數使用ByRef從主代碼修改原始變量。

enter image description here

Sub CheckRegister() 
    Dim A1 As Currency, A5 As Currency, A10 As Currency, A20 As Currency, A50 As Currency, A100 As Currency 
    Dim R1 As Currency, R5 As Currency, R10 As Currency, R20 As Currency, R50 As Currency, R100 As Currency 
    Dim rf As Currency 

    A1 = 36 
    A5 = 85 
    A10 = 50 
    A20 = 420 
    A100 = 0 
    rf = 175 

    TransferFunds rf, R1, A1, 1 
    TransferFunds rf, R5, A5, 5 
    TransferFunds rf, R10, A10, 10 
    TransferFunds rf, R20, A20, 20 
    TransferFunds rf, R50, A50, 50 
    TransferFunds rf, R100, A100, 100 

    RefundMessage "Refund Due", R1, "Ones", R5, "Fives", R10, "Tens", R20, "Twenties", R50, "Fifties", R100, "Hundreds" 
    RefundMessage "Remaining Register", A1, "Ones", A5, "Fives", A10, "Tens", A20, "Twenties", A50, "Fifties", A100, "Hundreds" 

End Sub 

Sub TransferFunds(ByRef rf As Currency, ByRef RValue, ByRef AValue As Currency, Denomination As Integer) 
    Dim RefundAmount As Currency 

    If rf > AValue Then 
     RValue = AValue 
    Else 
     RValue = RValue Mod Denomination 
    End If 

    AValue = AValue - RValue 
    rf = rf - RValue 

End Sub 

Sub RefundMessage(Title As String, ParamArray arValuePairs() As Variant) 
    Dim msg As String, Denomination As String 
    Dim i As Integer, cValue As Currency 

    For i = 0 To UBound(arValuePairs) Step 2 
     cValue = arValuePairs(i) 
     Denomination = arValuePairs(i + 1) 
     If cValue > 0 Then 
      msg = msg & FormatCurrency(cValue, 2) & " in " & Denomination & vbCrLf 
     End If 

    Next 

    MsgBox msg, vbInformation, "Refund Due" 
End Sub 
+0

謝謝!當我有機會時,我會檢查你的代碼。 – Eric