+ Reply to Thread
Results 1 to 3 of 3

Overflow

  1. #1
    Erik Beck Jensen
    Guest

    Overflow

    Excel 2003 SP2





    Hello every body


    This little sub gives me a runtime overflow. Did I miss something?




    Sub ebj()

    Dim ll_test As Long

    ll_test = 162 * 207


    End Sub



    I would have expected ll_test to take a value = 33,534. A Long should be
    sufficient to hold that. If I change the declaration to Single or Variant I
    still get an overflow.



    Any suggestions?





    Best regards

    Erik





  2. #2
    Andrew Taylor
    Guest

    Re: Overflow

    The problem is that VBA tries to calculate
    162 * 207 as an Integer, and then convert
    to the result to a Long. The first of these
    steps causes the overflow. You can get
    round this by forcing one of the values
    to be a Long:

    ll_test = 162& * 207
    or
    ll_test = CLng(162) * 207

    Note that ll_test = clng(162 * 207) will fail,
    for the same reason as in the original problem.


    Andrew Taylor




    Erik Beck Jensen wrote:
    > Excel 2003 SP2
    >
    > Hello every body
    >
    >
    > This little sub gives me a runtime overflow. Did I miss something?
    >
    >
    >
    >
    > Sub ebj()
    >
    > Dim ll_test As Long
    >
    > ll_test = 162 * 207
    >
    >
    > End Sub
    >
    >
    >
    > I would have expected ll_test to take a value = 33,534. A Long should be
    > sufficient to hold that. If I change the declaration to Single or Variant I
    > still get an overflow.
    >
    >
    >
    > Any suggestions?
    >
    >
    >
    >
    >
    > Best regards
    >
    > Erik



  3. #3
    Erik Beck Jensen
    Guest

    Re: Overflow

    Thanks you very much.

    The problem is solved.



    Best regards

    Erik





    "Andrew Taylor" <[email protected]> wrote in message
    news:[email protected]...
    > The problem is that VBA tries to calculate
    > 162 * 207 as an Integer, and then convert
    > to the result to a Long. The first of these
    > steps causes the overflow. You can get
    > round this by forcing one of the values
    > to be a Long:
    >
    > ll_test = 162& * 207
    > or
    > ll_test = CLng(162) * 207
    >
    > Note that ll_test = clng(162 * 207) will fail,
    > for the same reason as in the original problem.
    >
    >
    > Andrew Taylor
    >
    >
    >
    >
    > Erik Beck Jensen wrote:
    >> Excel 2003 SP2
    >>
    >> Hello every body
    >>
    >>
    >> This little sub gives me a runtime overflow. Did I miss something?
    >>
    >>
    >>
    >>
    >> Sub ebj()
    >>
    >> Dim ll_test As Long
    >>
    >> ll_test = 162 * 207
    >>
    >>
    >> End Sub
    >>
    >>
    >>
    >> I would have expected ll_test to take a value = 33,534. A Long should be
    >> sufficient to hold that. If I change the declaration to Single or Variant
    >> I
    >> still get an overflow.
    >>
    >>
    >>
    >> Any suggestions?
    >>
    >>
    >>
    >>
    >>
    >> Best regards
    >>
    >> Erik

    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1