Shimo
Shimo

Reputation: 1

VBA : Overflow error

I have an error i don't understand why

I have a function i didn't touch (without error) and now it's didn't work... My function has just on line

I tried the function CDbl() and use the double type variable

I get an Overflow error

Number = 1341.0937961001

NbDecimal = 2

Function Truncate(Number As Double, NbDecimal As Integer) As Double
    Truncate = Int(Number * 10 ^ NbDecimal) / (10 ^ NbDecimal)
End Function 'Error here : Overflow

I need help

Upvotes: 0

Views: 202

Answers (2)

Charles Williams
Charles Williams

Reputation: 23505

It will overflow when Number * 10 ^ nbDecimal exceeds 32767 because thats the largest number VBA will store in an integer.

Actually I am wrong: it will only overflow if you use CInt() rather than Int()

Upvotes: 1

Doug Coats
Doug Coats

Reputation: 7107

To add to What Charles Williams was saying, you might want to change your data type to Long.

Upvotes: 0

Related Questions