【发布时间】:2012-07-10 15:39:50
【问题描述】:
我有以下代码来检查输入到两个输入框中的值,如果两个值都为零,那么MsgBox 应该显示“停止!” (我稍后会更改为退出子,但我正在使用 MsgBox 进行测试)
从测试中我看到了这些结果:
两个字符串中的零都会产生预期的消息框。
第一个字符串中的非零值后跟第二个字符串中的任何非零值什么都不做(如预期的那样)。
第一个字符串中的零后跟等于或大于 10 的第二个字符串值会生成消息框(意外)。
我还注意到,如果第二个字符串是 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 中遇到的最有趣的问题,欢迎讨论。
编辑:我使用此代码在屏幕上显示参数以供最终用户查看。因此,我如何注意到 .00000000001% 问题:
.Range("D2").Value = "Min is " & 100 - MinPer * 100 & "%"
.Range("D3").Value = "Max is " & MaxPer * 100 - 100 & "%"
【问题讨论】:
-
你想测试什么?此外,将计算转换为字符串也会产生一些有趣的结果