簡體   English   中英

運行時錯誤“ 13”:類型不匹配

[英]Run-time error '13': Type Mismatch

我試圖創建一個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

在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具有完全相同的代碼,除了它稱為GetActiveRegen() ,它轉到Sheet2.Range("B3").Value = QtyEntry

當我選擇輸入框顯示的這兩個下拉選項中的一個並且可以輸入數字時,它會將那個數字放在它應該放在的位置,我遇到的問題是,如果我輸入一個數字,它將一直要求我輸入一個數字點擊取消,我得到錯誤13消息,如果我沒有回答,然后單擊“確定”,那么它將給出錯誤13消息。 這是我第一次在excel VBA中進行編程,並且我沒有真正的編程經驗,因此這真令人沮喪。 任何幫助將不勝感激。

該行顯示錯誤:

 QtyEntry = InputBox(MSG)

您在聲明中為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM