VBA - 如果将某些数字传递给另一个子程序,则会出现溢出错误

VBA - Overflow error if passing certain digits to another sub

我创建了一个用户窗体,我在其中输入数字或名称,然后它会根据此查找一些信息。如果输入是数字,它调用子 OpslagNummer 并传递来自用户窗体的输入。问题是它总是在某些数字 fx 61001 或 56001 上给我一个 "Overflow" 错误,其中 10001 工作得很好?

UserFrom 1 中的代码:

Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift 
As Integer)
If KeyCode = 13 Then
    UserForm1.Hide
    TextInput = TextBox1.Value
    UserForm1.TextBox1 = ""

    TextInput = 61001 '----Hardcoded number for the sake of debugging

    If IsNumeric(TextInput) = True Then
        Call OpslagNummer(TextInput)
    Else
        If Len(TextInput) >= 3 Then
            Call OpslagNavn(TextInput)
        Else
            End
        End If
    End If
End If
End Sub

模块 1 中的代码:

Sub OpslagNummer(ByVal TextBoxNumber As Integer)
    'Code that looks up the information
End Sub

您超出了最大整数值 (32,767),因此出现溢出。为了处理更大范围的参数,请使用 Long。您可以像这样转换模块 1:

Sub OpslagNummer(ByVal TextBoxNumber As Long)
    'Code that looks up the information
End Sub

这里是 VB(和 VBA​​)的基本数据类型参考:Visual Basic Data Types