vbabyval

VBA - Overflow error if passing certain digits to another sub


I've created a UserForm where I input a number or name, which will then look up some information based on this. If the input is numeric it calls the sub OpslagNummer and pass along the input from the UserForm. The problem is that it always gives me an "Overflow" error on certain number fx 61001 or 56001, where 10001 works just fine?

Code in 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

Code in Module1:

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

Solution

  • You get an overflow because you have surpassed the maximum Integer value (32,767). In order to handle larger range of arguments, use Long. You can convert your Module 1 like so:

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

    Here's a reference to basic data types for VB (and VBA): Visual Basic Data Types