【问题标题】:Unexpected String Results意外的字符串结果
【发布时间】: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 & "%"

【问题讨论】:

  • 你想测试什么?此外,将计算转换为字符串也会产生一些有趣的结果

标签: vba excel


【解决方案1】:

两件事

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)

【讨论】:

  • 输入非数值会产生类型不匹配错误,而不是not a valid input msgbox。 MinPer 和 MaxPer 的值不像预期的那样,我确定算术是正确的,但没有得到正确的值。例如 N1 = 5 和 N2 = 6 应该产生 MinPer = 0.95 和 MaxPer = 1.06 但都等于 1。
  • 刚下班,回家再讲这个。
  • 更新了帖子。请立即测试。
  • 仍然产生 MinPer 和 MaxPer = 1。现在为非数字输入获取正确的 msgbox 错误,但对于 0 输入没有停止 msgbox。
  • 使用Application.InputBox,它有一个Type 参数,可让您将输入限制为数字。您可以跳过IsNumeric 检查,但是您需要检查返回值是否为False(用户取消输入框)
【解决方案2】:

这是因为数字“10”在字符串(第二个字符)中有一个“0”,所以两者都为真。

试试这个:

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

为了获得额外的控制,保存用户输入(MinPer,MaxPer),然后在对它们执行任何数学运算之前给它们发送文本以确保有效性。

【讨论】:

    【解决方案3】:

    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() 函数的数据为零。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 2023-01-27
      • 2013-06-18
      相关资源
      最近更新 更多