运行时错误“13”:类型不匹配

时间:2014-08-13 09:26:33

标签: excel vba excel-vba runtime-error excel-2010

我正在尝试创建一个Excel工作表,当我选择一个下拉列表选项时,会显示一个输入框并要求用户输入一个数字。这是为了创建一个技能规划师"对于我玩的游戏。所以我正在使用代码

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Select Case Range("A4")

Case "Endurance"
Call Module1.GetEndurance
Case "Active Regeneration"
Call Module2.GetActiveRegen

End Select

End Sub
中的

和Module1中的

Sub GetEndurance()
Dim QtyEntry As Integer
Dim MSG As String
Const MinSkill As Integer = 0
Const MaxSkill As Integer = 100
MSG = "Please enter skill level between " & MinSkill & " and " & MaxSkill
Do
    QtyEntry = InputBox(MSG)
    If IsNumeric(QtyEntry) Then
        If QtyEntry >= MinSkill And QtyEntry <= MaxSkill Then Exit Do
        End If
            MSG = "... Really? I told you the valid options..."
            MSG = MSG & vbNewLine
            MSG = MSG & "Please enter skill level between " & MinSkill & " and " & MaxSkill
            Loop
Sheet2.Range("B2").Value = QtyEntry

End Sub

模块2具有完全相同的代码,除了它被称为GetActiveRegen()并且它转到Sheet2.Range("B3").Value = QtyEntry

当我选择输入框中显示的这两个下拉选项中的一个并且我可以输入一个数字时,它会将该数字放在它所假设的位置,我遇到的问题是它一直要求我输入一个数字,如果我点击取消我得到错误13消息,如果我没有回答并单击确定然后它给我错误13消息。这是我第一次在excel VBA中进行编程,而且我没有真正的编程经验,所以这非常令人沮丧。任何帮助将不胜感激。

此行显示错误:

 QtyEntry = InputBox(MSG)

1 个答案:

答案 0 :(得分:1)

您在声明中为QtyEntry选择了错误的数据类型。选择字符串而不是整数。

Sub GetEndurance()
Dim QtyEntry As String
Dim MSG As String
Const MinSkill As Integer = 0
Const MaxSkill As Integer = 100
MSG = "Please enter skill level between " & MinSkill & " and " & MaxSkill
Do
    QtyEntry = InputBox(MSG)
    If QtyEntry = "" Then Exit Do
    If IsNumeric(QtyEntry) Then
        If QtyEntry >= MinSkill And QtyEntry <= MaxSkill Then Exit Do
        End If
            MSG = "... Really? I told you the valid options..."
            MSG = MSG & vbNewLine
            MSG = MSG & "Please enter skill level between " & MinSkill & " and " & MaxSkill
            Loop
Sheet2.Range("B2").Value = QtyEntry

End Sub