2
\$\begingroup\$

I was writing some code that required the user to input a number, which was then stored in a variable. While testing the code I input a number larger than the variable type would allow and I got an variable overflow error. So I wrote this code to prevent that from happening. It works, but I was hoping there was a more concise way to do it, or even a function already part of VBA that I could use.

Sub PreventOverflow()

    Dim IsThisANumber As String
    Dim Number As Integer
    Dim PreventOverflow As Long
    Dim Cancel As VbMsgBoxResult

    Number = 0
    PreventOverflow = 0

    IsThisANumber = InputBox("Enter a number less than 32768")

    Do While Number = 0

        Do While PreventOverflow = 0
            If IsNumeric(IsThisANumber) = True Then
                If Len(IsThisANumber) < 6 Then
                    PreventOverflow = IsThisANumber
                Else
                    IsThisANumber = InputBox("Your number was not less than 32768" & vbNewLine & "Please enter a number less than 32768")
                    PreventOverflow = 0
                End If
            ElseIf IsThisANumber <> "" Then
                IsThisANumber = InputBox("Your did not enter a number" & vbNewLine & "Plese enter a number less than 32768")
                PreventOverflow = 0
            Else
                Cancel = MsgBox("You did not enter a number.  Do you want to continue?", vbYesNo)
                If Cancel = vbNo Then
                    Exit Sub
                Else
                    IsThisANumber = InputBox("Enter a number less than 32768")
                    PreventOverflow = 0
                End If
            End If
        Loop

        If PreventOverflow < 1 Or PreventOverflow > 32767 Then
            IsThisANumber = InputBox("Your number was not less than 32768" & vbNewLine & "Please enter a number less than 32768")
            PreventOverflow = 0
        Else
            Number = PreventOverflow
        End If
    Loop

End Sub
\$\endgroup\$
3
  • \$\begingroup\$ How are you calling this code? Shouldn't it be a Function procedure that returns the user input? \$\endgroup\$ Commented Oct 24, 2016 at 21:40
  • \$\begingroup\$ I actually haven't created my own functions, so I'm not familiar with that, but I know the basic idea. If I turned this code into a Function instead of a Sub would the it still work in the same way? I guess my question is, what is the difference between Function and Sub? But yes, ideally this would be a function, that I could call. \$\endgroup\$ Commented Oct 24, 2016 at 21:47
  • \$\begingroup\$ A Function is a procedure that returns a value to the caller, much like Len, IsNumeric, InputBox and MsgBox are. You would have to remove the Dim PreventOverflow As Long declaration though, because it having the same name as the function it's declared in would be a compile error (duplicate definition in current scope), because you assign a function's return value by assigning the function's identifier - so you can't have a local variable in a function with the same name as that function. \$\endgroup\$ Commented Oct 24, 2016 at 21:49

1 Answer 1

3
\$\begingroup\$

I think you over engineered this function a bit, which is easy to do. Simply store your value in a variant to check if it will fit in an integer.

 Private Function IsInteger(ByVal prompt As String) As Integer
    Dim longHolder As Variant
    longHolder = InputBox(prompt)
    If IsNumeric(longHolder) Then
        If Int(longHolder) = CDbl(longHolder) Then
            If Abs(longHolder) < 32768 Then
                IsInteger = longHolder
            Else
                MsgBox "Too Big"
                IsInteger = 0
            End If
        Else
            MsgBox "Decimals!"
            IsInteger = 0
        End If
    Else
        MsgBox "Not a number"
        IsInteger = 0
    End If
End Function

You would use it like this

Sub test()
    Dim x As Integer
    x = IsInteger("give me an integer")
End Sub

That being said, integers are obsolete. According to msdn VBA silently converts all integers to long. So, just use Long to begin with.

\$\endgroup\$
2
  • \$\begingroup\$ One more question. In the fifth line, why did you use Int(longHolder) = CDbl(longHolder) instead of just Int(longHolder) = longHolder? If the user entered a decimal, wouldn't VBA store the data in longHolder as a decimal? \$\endgroup\$ Commented Oct 26, 2016 at 7:04
  • \$\begingroup\$ @JacobBischoff because longHolder is a variant, when you enter a number, it's stored as a string. So your thinking is a very common thread (I fell into it writing this answer), but if you try it out with variant the int will not match the naked variable. Nor would the dbl match the naked variable. It holds true for both integers and decimals \$\endgroup\$ Commented Oct 26, 2016 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.