【问题标题】:Excel VBA Run-time error 13 - mismatch when box is blankExcel VBA 运行时错误 13 - 框为空白时不匹配
【发布时间】:2013-10-17 22:15:03
【问题描述】:

我被困在 VBA 中。我在网站上尝试了其他解决方案,但仍然无法正确解决。我正在使用多个模块和表单将一些信息输入到 Excel 的单元格中。但是,当 msgBox 留空时,它会给我一个 13 型不匹配错误。我试过 isNull 但不完全明白如何使用它。

任何帮助将不胜感激,并且请尽可能简单地回答任何问题,因为我充其量只是一个新手程序员。谢谢

Sub GetEndCostGateFees()
    Dim qtyGateFees As Double
    Dim msg As String

Const MinCost As Integer = 0
Const MaxCost As Integer = 200

msg = "Please enter the cost, per tonne, of Gate fees "

Do
    qtyGateFees = InputBox(msg, "Gate Fees")

    If IsNull(qtyGateFees) Then
        MsgBox ("Please enter a value. Enter 0 if none")
        End If

    If IsNumeric(qtyGateFees) Then
        If qtyGateFess >= MinCost And qtyGateFees <= MaxCost Then Exit Do
        End If
        msg = "Please enter a valid number"
        msg = msg & vbNewLine
        msg = msg & "Please enter number between " & MinCost & " and " & MaxCost
        Loop

Sheet25.Range("B43").Value = qtyGateFees

结束子

【问题讨论】:

  • Option Explicit 添加到模块的开头。这将使这里的问题之一变得明显。
  • 哪一行出现错误?

标签: vba excel isnullorempty


【解决方案1】:

如果您希望用户仅输入数字输入,请使用 Application.InputBoxType:=1

Sub sample()
    Dim Ret As Variant
    Dim msg

    msg = "Please enter the cost, per tonne, of Gate fees "

    Ret = Application.InputBox(msg, "Gatefees", Type:=1)

    If Ret <> False Then
        '
        '~~> Rest of your code here
        '
    End If
End Sub

【讨论】:

    【解决方案2】:

    将 qtyGateFees 更改为 Variant:

    Dim qtyGateFees As Variant
    

    我相信您遇到了类型不匹配错误,因为您试图将一个空白值分配给一个暗淡为“Double”的变量。

    然后你可以试试这个而不是 isNull:

    If qtyGateFees = "" Then
    

    【讨论】:

      【解决方案3】:

      您可以使用错误处理(无论如何这是一个好习惯),而不是将您的变量声明为 Variant

      Option Explicit
          Sub GetEndCostGateFees()
              Dim qtyGateFees As Double
              Dim msg As String
      
              Const MinCost As Integer = 0
              Const MaxCost As Integer = 200
      
              msg = "Please enter the cost, per tonne, of Gate fees "
      
              Do
                  On Error GoTo TypeM
                  qtyGateFees = InputBox(msg, "Gate Fees")
                  On Error GoTo 0
      
                  If IsNumeric(qtyGateFees) Then
                      If qtyGateFees >= MinCost And qtyGateFees <= MaxCost Then Exit Do
                  End If
                  msg = "Please enter a valid number"
                  msg = msg & vbNewLine
                  msg = msg & "Please enter number between " & MinCost & " and " & MaxCost
              Loop
      
              Sheets(Sheet25).Range("B43").Value = qtyGateFees
      
          Exit Sub
      
              TypeM:
              If Err.Number = 13 And Err.Description = "Type mismatch" Then
                  MsgBox "Please enter a value. Enter 0 if there were no fees."
                  Err.Clear
                  Resume
              Else
                  Debug.Print Err.Number & " " & Err.Description
              End If
      
          End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        相关资源
        最近更新 更多