vba 运行时错误“13”:类型不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25282663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run-time error '13': Type Mismatch
提问by RoseDeSang
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
在 ThisWorkbook 和内部 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
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").Value = 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 消息,如果我没有回答并单击确定,那么它会给我错误 13 消息。这是我第一次在 excel VBA 中进行编程,我没有真正的编程经验,所以这非常令人沮丧。任何帮助将不胜感激。
The error is showing on this line:
错误显示在这一行:
QtyEntry = InputBox(MSG)
回答by Jan Rothkegel
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