2012-07-10 27 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()函数。

相关问题