我有下面的代码检查值输入到两个输入框,如果这两个值都为零,则MsgBox
应显示“住手!” (稍后我会更改为退出子,但我使用测试一个MsgBox)有意想不到的字符串结果
从测试中我看到这些结果:
零两个字符串产生预期的消息框。
第一串接着是第二字符串中的任何非零值中的非零什么也不做(如预期)。
所述第一串之后是第二字符串值等于或大于10所述的0生成消息框(意外)。
我也注意到,如果第二个字符串是6-9,显示为x.00000000000001%
。我认为这是一个浮点问题,可能是相关的?此行为也不会发生IF... InStr
函数。
Option Explicit
Sub Models()
Dim MinPer As String, MaxPer As String, Frmula As String
Dim Data As Worksheet, Results As Worksheet
Set Data = Sheets("Data")
Set Results = Sheets("Results")
Application.ScreenUpdating = False
MinPer = 1 - InputBox("Enter Minimum Threshold Percentage, do not include the % symbol", _
"Minimum?")/100
MaxPer = 1 + InputBox("Enter Maximum Threshold Percentage, do not include the % symbol", _
"Maximum?")/100
If (InStr(MinPer, "0") = 0) And (InStr(MaxPer, "0") = 0) Then
MsgBox "STOP!"
End If
' Remainder of code...
这是迄今为止我在VBA中遇到过的最有趣的问题,欢迎大家对此进行讨论。
编辑:我使用此代码在屏幕上显示最终用户看到的参数。我因此,如何注意到0.00000000001%问题:
.Range("D2").Value = "Min is " & 100 - MinPer * 100 & "%"
.Range("D3").Value = "Max is " & MaxPer * 100 - 100 & "%"
你想要测试什么?同样,将计算转换为字符串也会产生一些有趣的结果 – SeanC 2012-07-10 15:47:11