0

我创建了一个用户窗体,我在其中输入了一个数字或名称,然后它将基于此查找一些信息。如果输入是数字,它调用子 OpslagNummer 并传递来自 UserForm 的输入。问题是它总是在某些数字 fx 61001 或 56001 上给我一个“溢出”错误,其中 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
4

1 回答 1

3

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

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

这是对 VB(和 VBA)的基本数据类型的参考:Visual Basic 数据类型

于 2019-08-12T07:40:19.300 回答