无法弄清楚为什么下面的代码在它假设的时候不会退出。至。任何帮助赞赏。问题是,只要值Do While
达到0,父RF
循环就不会结束循环。
我完全错了吗?不太熟悉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 :(得分:4)
如果您希望在处理循环时停止循环,则需要在每个新build 'Unit-Test'
之前进行检查,以检查Do While
是否等于或小于RF
:
0
答案 1 :(得分:2)
DragonSamu是对的;你应该在每个内循环之前添加<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<!--xsl:output method="xml" indent="yes"/-->
<xsl:strip-space elements="*"/>
<xsl:variable name="vReps" select="document('file:///c:/test/values.xml')"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- replace element text -->
<xsl:template match="SSTVal[text()='Version']">
<xsl:value-of select="$vReps"/>
</xsl:template>
</xsl:stylesheet>
。
这里我创建了一个if RF <= 0 then exit Do
来处理内部循环。 Function TransferFunds
中的参数使用ByRef来修改主代码中的原始变量。
TransferFunds
答案 2 :(得分:2)
Eric,如果我理解你,那么只有 希望执行进入内部循环,如果RF为正。一种简单的方法是将该要求明确地添加到所有内部循环中。例如,第一内环将是看起来像这样:
Do While RF > 0 And B1 = False'This is for the 1 dollar bills
如果你对其他三个内部循环进行类似的编辑,它可能会像我认为你想要的那样工作。 问候, 垫
答案 3 :(得分:0)
首先。外环将退出。但只有它到达各自的循环&#34;在代码中指出。所以RF将变为负数,内部循环将全部完成,然后外循环检查下一轮&#34;#34;的条件。
除了该行:
MsgBox "a20 " & RF > 0
您的代码将编译并运行,但可能不如您所期望的那样。因此,不应该问为什么你的代码不起作用,你应该也可以解释你想要实现的目标。