2012-07-10 25 views
3

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

你想要測試什麼?同樣,將計算轉換爲字符串也會產生一些有趣的結果 – SeanC 2012-07-10 15:47:11

回答

4

兩件事

1)聲明MinPerMaxPer作爲LongDouble,而不是作爲String您從計算存儲輸出

2)不要直接使用InputBox在計算中。它們存儲在一個變量,然後如果輸入是有效的,那麼用它們在計算

Dim MinPer As Double, MaxPer As Double, Frmula As String 
Dim Data As Worksheet, Results As Worksheet 
Dim n1 As Long, n2 As Long 

Set Data = Sheets("Data") 
Set Results = Sheets("Results") 

Application.ScreenUpdating = False 

On Error Resume Next 
n1 = Application.InputBox(Prompt:="Enter Minimum Threshold Percentage, do not include the % symbol", _ 
Title:="Minimum?", Type:=1) 
On Error GoTo 0 

If n1 = False Then 
    MsgBox "User cancelled" 
    Exit Sub 
End If 

On Error Resume Next 
n2 = Application.InputBox(Prompt:="Enter Maximum Threshold Percentage, do not include the % symbol", _ 
Title:="Maximum?", Type:=1) 
On Error GoTo 0 

If n2 = False Then 
    MsgBox "User cancelled" 
    Exit Sub 
End If 

If n1 = 0 And n2 = 0 Then 
    MsgBox "STOP!" 
End If 

MinPer = 1 - (Val(n1)/100) 
MaxPer = 1 + (Val(n2)/100) 
+0

輸入非數字值會產生類型不匹配錯誤,而不是「無效的輸入」msgbox。 MinPer和MaxPer的值並不如預期,我敢肯定算術是正確的,但沒有得到正確的值。例如N1 = 5和N2 = 6應該產生MinPer = 0.95和MaxPer = 1.06,但都等於1. – 2012-07-10 16:03:14

+0

剛離開辦公室時,會回到這個時候回家。 – 2012-07-10 16:06:58

+0

更新了帖子。請現在測試它。 – 2012-07-10 16:11:26

1

這是因爲「10」數字的字符串(第二個字符)有一個「0」,所以雙方計算結果爲true。

試試這個:

If (MinPer = "0") And (MaxPer = "0") Then 
    MsgBox "STOP!" 
End If 

對於額外的控制保存用戶輸入(MinPer,MAXPER),然後對他們進行拒絕數學運算之前文本其有效性。

0

InStr函數(MinPer,「0」)只是檢查,以查看該字符串是否包含零 字符。

您需要將字符串值轉換爲整數。使用IsNumeric和CInt函數 來做到這一點。看到這個網址:

vba convert string to int if string is a number

Dim minPerINT as Integer 
Dim maxPerINT as Integer 

If IsNumeric(minPer) Then 
    minPerINT = CInt(minPer) 
Else 
    minPerINT = 0 
End If 
If IsNumeric(maxPer) Then 
    maxPerINT = CInt(maxPer) 
Else 
    maxPerINT = 0 
End If 

If minPerINT = 0 and maxPerINT=0 Then 
    MsgBox "STOP!" 
End If 

根據什麼可以輸入數據也可能是一個好主意,檢查數據的長度 爲零使用LEN()函數。

相關問題