2013-10-17 105 views
3

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

任何帮助将不胜感激,请保持尽可能简单的答案,因为我是一个新手程序员充其量。由于

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 

末次

+2

将'Option Explicit'添加到模块的开头。这会使问题变得明显。 – RBarryYoung

+0

你在哪一行得到错误? – ARich

回答

0

变化qtyGateFees变型方案:

Dim qtyGateFees As Variant 

我相信你要因为你想为空值赋给变量的类型不匹配错误被淡化为“双”。

然后,您可以试试这个,而不是的isNull:

If qtyGateFees = "" Then 
4

如果你希望用户只输入数字输入,然后使用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 
0

相反的声明你的变量为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 
相关问题