+ Reply to Thread
Results 1 to 4 of 4

How can I implement the Modulo function in VBA?

  1. #1
    Registered User
    Join Date
    12-05-2006
    Location
    Philadelphia PA
    MS-Off Ver
    Office 365
    Posts
    81

    How can I implement the Modulo function in VBA?

    VBA 2003 does not support MOD (modulo arithmetic) as a WorksheetFunction, and there does not seem to be a direct MOD function in VBA. I've been dabbling with code that would do the same thing as MOD, so far with no success. Does anyone know how to do this in VBA? I assume it would be some combination of If...Then, Round (up or down), maybe a compare, and perhaps an iteration loop. I would think such a routine would be useful to many people.

    Thanks in advance.
    Last edited by batman; 10-28-2009 at 03:51 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How can I implement the Modulo function in VBA?

    Hello batman,

    Read the VBA Help files...

    Mod Operator


    Used to divide two numbers and return only the remainder.

    Syntax

    result = number1 Mod number2

    The Mod operator syntax has these parts:

    Part Description
    result Required; any numeric variable.
    number1 Required; any numeric expression.
    number2 Required; any numeric expression.



    Remarks

    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

    Usually, the data type of result is a Byte, Byte variant, Integer, Integer variant, Long, or Variant containing a Long, regardless of whether or not result is a whole number. Any fractional portion is truncated. However, if any expression is Null, result is Null. Any expression that is Empty is treated as 0.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How can I implement the Modulo function in VBA?

    VBA Mod handles negative numbers differently than the worksheet function MOD.

    =MOD(-1, 3) returns 2
    Please Login or Register  to view this content.
    returns -1
    Please Login or Register  to view this content.
    will return the same value as =MOD(a,b)
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    12-05-2006
    Location
    Philadelphia PA
    MS-Off Ver
    Office 365
    Posts
    81

    Re: How can I implement the Modulo function in VBA?

    Leith & Mike

    Thanks. It certainly seemed as though VBA ought to be able to do something as simple and useful as modulo arithmetic. Obviously I didn't do a very thorough job of searching the VBA help. I'll mark the thread solved.

+ 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