2013-10-17 106 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 
相關問題