+ Reply to Thread
Results 1 to 9 of 9

[vba] mod function not working for variables bigger than 'long'

  1. #1
    Registered User
    Join Date
    09-24-2017
    Location
    London, England
    MS-Off Ver
    Excel 2016
    Posts
    15

    [vba] mod function not working for variables bigger than 'long'

    Hi all,

    This is my first post here - so apologies if I don't write things in the best way.

    Basically I'm writing a short VBA program to determine all of the prime factors of a given number (let's call this x). I'm pretty happy with the general logic but one thing I'm struggling on is a step where I determine whether or not a number (from 1 to sqrt(X)) is a factor.

    Originally, I did:

    If x/i = round(x/i,0) Then [...]

    Which worked until x was > 2.1b (the maximum Long number), however x was defined as Single or Double.

    Next, I then looked to:

    If x mod i = 0 Then [...]

    And the mod function seemed a lot quicker, but again broken when x exceeded the maximum Long value, despite being defined as Single or Double.

    I might be missing something obvious here, but would love to know of any solutions to get around this, as I think the logic is sound.

    Thanks!
    Alex
    Last edited by AB91; 09-24-2017 at 05:41 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: [vba] mod function not working for variables bigger than 'long'

    Welcome to the board.

    In VBA, Mod is an operator that applies only to Longs. For Doubles, you can use

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-24-2017
    Location
    London, England
    MS-Off Ver
    Excel 2016
    Posts
    15

    Re: [vba] mod function not working for variables bigger than 'long'

    Hi shg,

    Thanks for the reply. I did also try that but still the error persists. I tried it looking at 10,000,000,000 using:

    If Int(x / i) = x / i Then

    MsgBox x / i

    I put in the msgbox so I could check the value and x/i was being calculated as an integer, so int(x/i) did always equal x/i. Am I doing something stupid?

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: [vba] mod function not working for variables bigger than 'long'

    What numbers did you use that you believe were in error?

    Post all of your code.

  5. #5
    Registered User
    Join Date
    09-24-2017
    Location
    London, England
    MS-Off Ver
    Excel 2016
    Posts
    15

    Re: [vba] mod function not working for variables bigger than 'long'

    Please Login or Register  to view this content.
    This was so in K it noted down the factors of the number. I know I said prime factors but I'll incorporate that once I fix what's currently there.

    If i use x = 10b and just loop through from y-5 to 5 (for speed) then the output is 99995
    99996
    99997
    99998
    99999
    100000

    And only the last one is actually a factor.

    Thanks again, I really appreciate this!

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: [vba] mod function not working for variables bigger than 'long'

    What's in B2?

  7. #7
    Registered User
    Join Date
    09-24-2017
    Location
    London, England
    MS-Off Ver
    Excel 2016
    Posts
    15

    Re: [vba] mod function not working for variables bigger than 'long'

    Ah sorry, B2 was 10,000,000,000 - everything there works as expected when B2 is less than the maximum Long value

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: [vba] mod function not working for variables bigger than 'long'

    Number on worksheets are Double, not Singles, and a Single only has a 23-bit (IIRC) mantissa, which is not long enough to store 10,000,000,000 as an integer.

    Try this:

    Please Login or Register  to view this content.
    E.g.,

    A
    B
    C
    D
    E
    1
    10,000,000,000
    2
    10,000,000,000
    D1: =PRODUCT(B1:B23)
    2
    2
    3
    2
    4
    2
    5
    2
    6
    2
    7
    2
    8
    2
    9
    2
    10
    2
    11
    5
    12
    5
    13
    5
    14
    5
    15
    5
    16
    5
    17
    5
    18
    5
    19
    5
    20
    5

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: [vba] mod function not working for variables bigger than 'long'

    I did this:

    Please Login or Register  to view this content.
    and got:

    Data Range
    J
    K
    L
    M
    2
    10000000
    1
    10000000
    3
    10000000
    2
    5000000
    4
    10000000
    4
    2500000
    5
    10000000
    5
    2000000
    6
    10000000
    8
    1250000
    7
    10000000
    10
    1000000
    8
    10000000
    16
    625000
    9
    10000000
    20
    500000
    10
    10000000
    25
    400000
    11
    10000000
    32
    312500
    12
    10000000
    40
    250000
    13
    10000000
    50
    200000
    14
    10000000
    64
    156250
    15
    10000000
    80
    125000
    16
    10000000
    100
    100000
    17
    10000000
    125
    80000
    18
    10000000
    128
    78125
    19
    10000000
    160
    62500
    20
    10000000
    200
    50000
    21
    10000000
    250
    40000
    22
    10000000
    320
    31250
    23
    10000000
    400
    25000
    24
    10000000
    500
    20000
    25
    10000000
    625
    16000
    26
    10000000
    640
    15625
    27
    10000000
    800
    12500
    28
    10000000
    1000
    10000
    29
    10000000
    1250
    8000
    30
    10000000
    1600
    6250
    31
    10000000
    2000
    5000
    32
    10000000
    2500
    4000
    33
    10000000
    3125
    3200
    Last edited by xladept; 09-24-2017 at 05:05 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Step Through Function (F8 command) and Checking Variables -- Not working?
    By NicholasL in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-20-2013, 11:11 AM
  2. [SOLVED] Can not write Long variables to worksheet = what am I doing wrong???
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-14-2013, 07:59 PM
  3. [SOLVED] Passing Long variables in date format
    By vvolis in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2012, 07:46 AM
  4. [SOLVED] How long do variables retain their values
    By comparini3000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2006, 12:35 PM
  5. [SOLVED] Len function returns bigger number
    By Khoshravan in forum Excel General
    Replies: 10
    Last Post: 05-21-2006, 03:40 AM
  6. Replies: 2
    Last Post: 03-14-2006, 02:04 AM
  7. Transpose Function not Working with Long Array Elements
    By Ngan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-09-2005, 08:05 PM
  8. how to prevent worksheet scroll bar to be bigger than working are.
    By Worksheet AREA in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-16-2005, 03:06 PM

Tags for this Thread

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