0

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

3
  • 1
    I tried running this with the values you provided and it works. So can you supply the code you use to run this? Commented Jan 29, 2016 at 11:36
  • I, too, cannot reproduce the error using the code and values in your post. Commented Jan 29, 2016 at 12:58
  • Sorry the problem has not this function but the next. I resolve the problem Commented Jan 29, 2016 at 14:54

2 Answers 2

1

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()

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

1 Comment

I cannot reproduce that using his code. With his code and his values, Number * 10 ^ nbDecimal = 134109.37961001 and the code runs without error. I would expect a problem if nbDecimal > 32767 (Excel 2016)
0

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

Comments

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.