我有下面的代碼檢查值輸入到兩個輸入框,如果這兩個值都爲零,則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