Excel VBA-不一致条件评估

Excel VBA-不一致条件评估,excel,while-loop,vba,Excel,While Loop,Vba,不一致条件评价 有时,当条件应评估为False时,While循环会额外执行一次。 例如,如果输入1作为开始,输入3作为结束,输入0.05作为步骤 Private Sub CommandButton21_Click() Dim StrPrompt As String Dim StepValue As Double Dim StartValue As Double Dim EndValue As Double Dim CurrentValue As Double StrPrompt = "

不一致条件评价 有时,当条件应评估为
False
时,
While
循环会额外执行一次。 例如,如果输入1作为开始,输入3作为结束,输入0.05作为步骤

    Private Sub CommandButton21_Click()
Dim StrPrompt As String
Dim StepValue As Double
Dim StartValue As Double
Dim EndValue As Double
Dim CurrentValue As Double

StrPrompt = "Enter a Positive start number."
redo:
StartValue = Application.InputBox(StrPrompt, "Start", , , , , , Type:=1)
If Not (StartValue >= 0) Then
    StrPrompt = "Please enter a postive number."
    GoTo redo
End If

MsgBox "User entered " & StartValue, , "Start Value"
Cells(1, 4) = StartValue & " Start"


StrPrompt = "Enter a Positive end number."
redo1:
EndValue = Application.InputBox(StrPrompt, "End", , , , , , Type:=1)
If EndValue <= StartValue Then
    StrPrompt = "Please enter a number larger than start number."
    GoTo redo1
End If

MsgBox "User entered " & EndValue, , "End Value"
Cells(1, 4) = EndValue & " End"

StrPrompt = "Enter a Positive step value."
redo2:
StepValue = Application.InputBox(StrPrompt, "Step", , , , , , Type:=1)
If Not (StepValue > 0) Then
    StrPrompt = "Please enter a number > 0."
    GoTo redo2
End If


MsgBox "User entered " & StepValue, , "Step Value"
Cells(1, 4) = StartValue & " Step"

CurrentValue = StartValue
Cells(1, 4) = CurrentValue

   While CurrentValue < EndValue

   ' MsgBox "Current Value before increment " & CurrentValue, , "Before"
    CurrentValue = CurrentValue + StepValue
    'MsgBox "Current Value after increment " & CurrentValue, , "After"
    Cells(1, 4) = CurrentValue
   Wend

End Sub
Private子命令按钮21_Click()
作为字符串的Dim StrPrompt
将步长值设置为双精度
暗淡的起始值为双精度
Dim EndValue为双精度
将当前值设置为双精度
strcompt=“输入一个正的起始编号。”
重做:
StartValue=Application.InputBox(strCompt,“开始”,,类型:=1)
如果不是(起始值>=0),则
strport=“请输入一个邮政编码。”
转到重做
如果结束
MsgBox“用户输入”和StartValue“开始值”
单元格(1,4)=起始值和“开始”
strcompt=“输入一个正的结束编号。”
第1条:
EndValue=Application.InputBox(strCompt,“End”,,类型:=1)
如果EndValue为0),则
strcompt=“请输入一个大于0的数字。”
转到redo2
如果结束
MsgBox“用户输入”和步骤值,“步骤值”
单元格(1,4)=起始值和“步长”
CurrentValue=起始值
单元格(1,4)=当前值
当CurrentValue
我们需要更多信息。对于问题中所述的输入,在每个循环迭代中,
单元格(1,4)
中放置的内容是什么?1,1.05,1.10…然后在3.05结束浮点精度限制可能会导致此类错误。考虑用Wrand()来打发你的条件。谢谢,这似乎解决了这个问题,虽然看起来有点疯狂,但是这么小的数字会使Excel超标。