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

时间:2022-09-13 17:04:34

I am trying to create an excel sheet in which when I select a drop down list option an input box shows up and asks the user to enter a number. This is to create a "skill planner" for a game I play. So I am using the code

我正在尝试创建一个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

in ThisWorkbook and inside Module1 is

在这本工作簿和内部模块1是

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

Module2 has the exact same code except it's called GetActiveRegen() and it goes to Sheet2.Range("B3").Value = QtyEntry.

Module2有完全相同的代码,只是它被称为GetActiveRegen(),并进入Sheet2.Range(“B3”)。值= QtyEntry。

When I select one of these two drop down options that input box shows up and I can put in a number, it puts that number where it's suppose to, the problem I have is that it keeps asking me to put in a number, if I hit cancel I get the error 13 message, if I put in no answer and click ok then it gives me the error 13 message. This is my first time doing programming in excel VBA and I have no real programming experience so this is getting highly frustrating. Any help will be greatly appreciated.

当我选择这两个下拉选项,输入框显示我可以放进一个数字,它把这个数字是假设,我的问题是,它使让我输入一个数字,如果点击取消我得到错误13日消息,如果我不回答,然后单击ok 13给我错误的信息。这是我第一次用excel VBA编程,我没有真正的编程经验,这让我非常沮丧。如有任何帮助,我们将不胜感激。

The error is showing on this line:

错误显示在这一行:

 QtyEntry = InputBox(MSG)

1 个解决方案

#1


1  

You've chosen the wrong datatype for QtyEntry in the declaration. Choose string instead of integer.

您在声明中为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

#1


1  

You've chosen the wrong datatype for QtyEntry in the declaration. Choose string instead of integer.

您在声明中为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