0

I have encountered this error even though all my data types seems fine.

Run-time error 6 Overflow

Here is the function:

Function equation(x As Long) As Long

    Dim a As Long, b As Double

    a = Int(((x - 2) Mod 8) / 6) + 2 * Int((x - 2) / 8)
    b = (x + a - 1) / 2

    equation = Abs(4 * b + 5 + 2 * Int(b))

End Function

Error is encountered when x = 572662307 it says overflow.

6
  • 1
    Are you sure you want to use the Int function? It performs integer-truncation. Commented Feb 14, 2018 at 7:38
  • 572662307 is one-quarter towards the max value of a 32-bit signed integer (2 billion). In VBA Long is a 32-bit integer and is not a 64-bit integer as in C#. I recommend changing x to Double. Commented Feb 14, 2018 at 7:42
  • @Dai what is the substitute for this int? yes it is necessary cause i need only the integer part of the number after mod Commented Feb 14, 2018 at 7:42
  • maybe LongLong data type will do Commented Feb 14, 2018 at 8:08
  • @DisplayName LongLong is not available in 32-bit Office. Commented Feb 14, 2018 at 8:21

1 Answer 1

3

x = 572662307. is one-quarter of the max value of a 32-bit signed integer (~2 billion) so your arithmetic operations will likely hit that indeed.

In VBA Long is a 32-bit signed integer and is not a 64-bit signed integer as in C#. I recommend changing both a and x to Double.

You could also spread-out your function so you can inspect all intermediate steps in your debugger:

Function equation(x As Double) As Double

    Dim a As Double, b As Double, c As Double, d As Double, e As Double, f As Double, g As Double

    a = (x - 2) Mod 8
    b = a / 6
    b = Int( b )

    c = (x - 2) / 8
    d = 2 * Int( c )

    e = b + d
    f = (x + a - 1)
    f = f / 2

    g = 4 * b + 5 + 2 * Int( f )
    equation = Abs( g )

End Function
Sign up to request clarification or add additional context in comments.

1 Comment

but x is also >2billion also for a

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.