Can not figure out why the following code doesn't exit when it's 'supposed' to. Any help appreciated. The issue is the Parent Do While loop is not ending the loop as soon as the value RF hits 0.
Did I set this up completely wrong? Not too familiar with VBA, and haven't written a program in any language in a few years now so I'm very rusty.
Thank you in advance for any insight!
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
Note that I edited out some code that was working/doesn't pertain to the issue at hand.
And yes, I have a ton of variables. I know there's probably a much easier way to code what I'm attempting to do.
