+ Reply to Thread
Results 1 to 8 of 8

Mod Function Returning Long

  1. #1
    Jim Thomlinson
    Guest

    Mod Function Returning Long

    What am I missing here...

    377.25 Mod 25 is returning 2 instead of 2.25. In the watch window the result
    of the function is a long. Everything I have read I should be getting a
    double.

    Sub ModTest()
    MsgBox 377.25 Mod 25
    End Sub

    Jim Thomlinson

  2. #2
    Bob Phillips
    Guest

    Re: Mod Function Returning Long

    Jim,

    The answer in in Help

    The modulus, or remainder, operator divides number1 by number2 (rounding
    floating-point numbers to integers)

    Note the bit in brackets. VBA Mod behaves differently to Excel Mod.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    news:E8651F03-7535-4F88-96B5-302ABB35B629@microsoft.com...
    > What am I missing here...
    >
    > 377.25 Mod 25 is returning 2 instead of 2.25. In the watch window the

    result
    > of the function is a long. Everything I have read I should be getting a
    > double.
    >
    > Sub ModTest()
    > MsgBox 377.25 Mod 25
    > End Sub
    >
    > Jim Thomlinson




  3. #3
    Bernie Deitrick
    Guest

    Re: Mod Function Returning Long

    Jim,

    In Visual Basic 6.0, the MOD operator accepted any numeric expression and the result was always
    returned as an integer. I have to assume that the same is true in VBA.

    Try using:

    MsgBox 377.25 - Int(377.25 / 25) * 25

    HTH,
    Bernie
    MS Excel MVP



    "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    news:E8651F03-7535-4F88-96B5-302ABB35B629@microsoft.com...
    > What am I missing here...
    >
    > 377.25 Mod 25 is returning 2 instead of 2.25. In the watch window the result
    > of the function is a long. Everything I have read I should be getting a
    > double.
    >
    > Sub ModTest()
    > MsgBox 377.25 Mod 25
    > End Sub
    >
    > Jim Thomlinson




  4. #4
    Rob Bovey
    Guest

    Re: Mod Function Returning Long

    Hi Jim,

    Here's an excerpt from the help topic on the VBA Mod function:

    ------------------------------------------------
    The modulus, or remainder, operator divides number1 by number2 (rounding
    floating-point numbers to integers) and returns only the remainder as
    result. For example, in the following expression, A (result) equals 5.

    A = 19 Mod 6.7
    ------------------------------------------------

    --
    Rob Bovey, Excel MVP
    Application Professionals
    http://www.appspro.com/

    * Take your Excel development skills to the next level.
    * Professional Excel Development
    http://www.appspro.com/Books/Books.htm

    "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    news:E8651F03-7535-4F88-96B5-302ABB35B629@microsoft.com...
    > What am I missing here...
    >
    > 377.25 Mod 25 is returning 2 instead of 2.25. In the watch window the
    > result
    > of the function is a long. Everything I have read I should be getting a
    > double.
    >
    > Sub ModTest()
    > MsgBox 377.25 Mod 25
    > End Sub
    >
    > Jim Thomlinson




  5. #5
    Jim Thomlinson
    Guest

    Re: Mod Function Returning Long

    I did not see that anywhere in the help... Must not have looked hard
    enough... In MSDN help for VB here is what they say... If number1 or number2
    are floating-point values, then division is carried out and the
    floating-point remainder is returned. I guess I will have to work around it...
    --
    HTH...

    Jim Thomlinson


    "Bob Phillips" wrote:

    > Jim,
    >
    > The answer in in Help
    >
    > The modulus, or remainder, operator divides number1 by number2 (rounding
    > floating-point numbers to integers)
    >
    > Note the bit in brackets. VBA Mod behaves differently to Excel Mod.
    >
    > --
    >
    > HTH
    >
    > RP
    > (remove nothere from the email address if mailing direct)
    >
    >
    > "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    > news:E8651F03-7535-4F88-96B5-302ABB35B629@microsoft.com...
    > > What am I missing here...
    > >
    > > 377.25 Mod 25 is returning 2 instead of 2.25. In the watch window the

    > result
    > > of the function is a long. Everything I have read I should be getting a
    > > double.
    > >
    > > Sub ModTest()
    > > MsgBox 377.25 Mod 25
    > > End Sub
    > >
    > > Jim Thomlinson

    >
    >
    >


  6. #6
    Bernie Deitrick
    Guest

    Re: Mod Function Returning Long

    Jim,

    That is true of VB.NET, but not VBA.

    "In Visual Basic .NET, the MOD operator no longer accepts variants, and if either operand is a
    floating-point number, the result will be a floating-point number."

    HTH,
    Bernie
    MS Excel MVP


    "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    news:5540B74A-62A7-4D97-8C8D-27FC1E8634EC@microsoft.com...
    >I did not see that anywhere in the help... Must not have looked hard
    > enough... In MSDN help for VB here is what they say... If number1 or number2
    > are floating-point values, then division is carried out and the
    > floating-point remainder is returned. I guess I will have to work around it...
    > --
    > HTH...
    >
    > Jim Thomlinson
    >
    >
    > "Bob Phillips" wrote:
    >
    >> Jim,
    >>
    >> The answer in in Help
    >>
    >> The modulus, or remainder, operator divides number1 by number2 (rounding
    >> floating-point numbers to integers)
    >>
    >> Note the bit in brackets. VBA Mod behaves differently to Excel Mod.
    >>
    >> --
    >>
    >> HTH
    >>
    >> RP
    >> (remove nothere from the email address if mailing direct)
    >>
    >>
    >> "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    >> news:E8651F03-7535-4F88-96B5-302ABB35B629@microsoft.com...
    >> > What am I missing here...
    >> >
    >> > 377.25 Mod 25 is returning 2 instead of 2.25. In the watch window the

    >> result
    >> > of the function is a long. Everything I have read I should be getting a
    >> > double.
    >> >
    >> > Sub ModTest()
    >> > MsgBox 377.25 Mod 25
    >> > End Sub
    >> >
    >> > Jim Thomlinson

    >>
    >>
    >>




  7. #7
    Jim Thomlinson
    Guest

    Re: Mod Function Returning Long

    And you learn something new every day... Thanks guys...
    --
    HTH...

    Jim Thomlinson


    "Bernie Deitrick" wrote:

    > Jim,
    >
    > In Visual Basic 6.0, the MOD operator accepted any numeric expression and the result was always
    > returned as an integer. I have to assume that the same is true in VBA.
    >
    > Try using:
    >
    > MsgBox 377.25 - Int(377.25 / 25) * 25
    >
    > HTH,
    > Bernie
    > MS Excel MVP
    >
    >
    >
    > "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    > news:E8651F03-7535-4F88-96B5-302ABB35B629@microsoft.com...
    > > What am I missing here...
    > >
    > > 377.25 Mod 25 is returning 2 instead of 2.25. In the watch window the result
    > > of the function is a long. Everything I have read I should be getting a
    > > double.
    > >
    > > Sub ModTest()
    > > MsgBox 377.25 Mod 25
    > > End Sub
    > >
    > > Jim Thomlinson

    >
    >
    >


  8. #8
    Jim Thomlinson
    Guest

    Re: Mod Function Returning Long

    I knew I hung out around here for a reason... Answers above and beyond the
    call of duty...
    --
    HTH...

    Jim Thomlinson


    "Bernie Deitrick" wrote:

    > Jim,
    >
    > That is true of VB.NET, but not VBA.
    >
    > "In Visual Basic .NET, the MOD operator no longer accepts variants, and if either operand is a
    > floating-point number, the result will be a floating-point number."
    >
    > HTH,
    > Bernie
    > MS Excel MVP
    >
    >
    > "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    > news:5540B74A-62A7-4D97-8C8D-27FC1E8634EC@microsoft.com...
    > >I did not see that anywhere in the help... Must not have looked hard
    > > enough... In MSDN help for VB here is what they say... If number1 or number2
    > > are floating-point values, then division is carried out and the
    > > floating-point remainder is returned. I guess I will have to work around it...
    > > --
    > > HTH...
    > >
    > > Jim Thomlinson
    > >
    > >
    > > "Bob Phillips" wrote:
    > >
    > >> Jim,
    > >>
    > >> The answer in in Help
    > >>
    > >> The modulus, or remainder, operator divides number1 by number2 (rounding
    > >> floating-point numbers to integers)
    > >>
    > >> Note the bit in brackets. VBA Mod behaves differently to Excel Mod.
    > >>
    > >> --
    > >>
    > >> HTH
    > >>
    > >> RP
    > >> (remove nothere from the email address if mailing direct)
    > >>
    > >>
    > >> "Jim Thomlinson" <jamest@tcgiRe-Move-This.com> wrote in message
    > >> news:E8651F03-7535-4F88-96B5-302ABB35B629@microsoft.com...
    > >> > What am I missing here...
    > >> >
    > >> > 377.25 Mod 25 is returning 2 instead of 2.25. In the watch window the
    > >> result
    > >> > of the function is a long. Everything I have read I should be getting a
    > >> > double.
    > >> >
    > >> > Sub ModTest()
    > >> > MsgBox 377.25 Mod 25
    > >> > End Sub
    > >> >
    > >> > Jim Thomlinson
    > >>
    > >>
    > >>

    >
    >
    >


+ 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