+ Reply to Thread
Results 1 to 12 of 12

VBA Loop Until cell = integer

  1. #1
    Registered User
    Join Date
    02-05-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    4

    Question VBA Loop Until cell = integer

    Hello All,

    First time posting here but I am stuck. Normally I can tie together parts from amazing responses here to figure it out but not this time. What I want to accomplish it to reduce a cell value by 1 until an equation equals a full integer. Replacing C1 below with A1 -1 until C1 is an integer.

    A1=500
    B1=15
    C1= cell starting at 500 (or A1) reducing by 1 until D1 = integer
    D1 = ((A1/2)/B1)*100)

    492 is what C1 should say and D1 should be 1640 if the loop runs correctly

  2. #2
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: VBA Loop Until cell = integer

    I cannot get your expected result
    498/2/50 = 16,6
    16,6*100 = 1660
    Please Login or Register  to view this content.
    cheers

  3. #3
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Loop Until cell = integer

    For that to be the case, the formula in D1 would need to be

    =(C1/2)/B1)*100

    Anyway, the value in C1 just needs to be a multiple of 3: (1/2)*(1/15)*100 simplifies to 10/3, so every third number will meet that criteria - more precisely, a number that is a multiple of 3. Your code, however, needs to account for the floating point errors inherent to binary code: you may think that 492 is the first to meet your criteria, but 498 and 495 do that before 492.

    Unless you are using weird numbers, then logic is the easiest way to solve your problem, not looping code.
    Bernie Deitrick
    Excel MVP 2000-2010

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA Loop Until cell = integer

    Quote Originally Posted by Bernie Deitrick View Post
    Anyway, the value in C1 just needs to be a multiple of 3:
    One might presume that A1 and B1 could change arbitrarily.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: VBA Loop Until cell = integer

    According to your formula, 498 in cell A1 gives a whole number 1660 as a result by hand calculating according to your formula in cell D1, before the value in cell A1 is reduced further more.

    This result can be achieved by VBA with the following Do-Loop;


    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Loop Until cell = integer

    True, but the logical requirements for any combination of linear, integer factors to produce an integer value should be relatively easy to determine.

  7. #7
    Registered User
    Join Date
    02-05-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    4

    Re: VBA Loop Until cell = integer

    Wait this is actually better! The closer to 500 the better!! Basically the value needs to be as close to 500 or 600 or whatever the number happens to be but the 1660 is actually $16.60 so I figured making it times 100 and checking if it was an integer was easier. Another would be A1=1600, B1=140. How would I get it to paste the result into a cell?

    The numbers for A1 and B1 could be anything. THis is for quoting to make sure the value doesn't do more than two decimal places (hence the idea of multiplying and using an integer).

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Loop Until cell = integer

    X * (1/2)*(1/140)*100 reduces to X * 100/280 to X * 10/28 to X * 5/14 so that will be an integer when X is a multiple of 14: The max value is INT(1600/14)*14 = 1596
    Last edited by Bernie Deitrick; 04-07-2020 at 05:05 PM.

  9. #9
    Registered User
    Join Date
    02-05-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    4

    Re: VBA Loop Until cell = integer

    How would I have it input 1660 into a cell?

  10. #10
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Loop Until cell = integer

    Once you have figured out how to reduce the multiplying factor to its lowest rational number, enter the denominator Z into a cell - say, C2 - and use the formula

    =INT(A1/C2)*C2

    or since you really want Dollars and Cents:

    =INT(A1/C2)*C2/100

  11. #11
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: VBA Loop Until cell = integer

    @Koehlr2; you can try this... (A1=500 and B1=15 or A1=1600 and B1=140 or whatever the values are...)

    Please Login or Register  to view this content.
    Last edited by Haluk; 04-07-2020 at 05:47 PM.

  12. #12
    Registered User
    Join Date
    02-05-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    4

    Re: VBA Loop Until cell = integer

    Haluk, That did the trick! Thank you all for the support!!!!

+ 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. Loop thru all .xlsx in a directory, compare & select one with largest integer in filename
    By potatomaster in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-21-2018, 01:40 PM
  2. [SOLVED] Loop on the Row as integer, getting integer into a range
    By Fean in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2018, 07:16 AM
  3. Create a loop by defining X as integer, to shorten length of code
    By ahmerjaved in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-11-2013, 01:11 PM
  4. [SOLVED] Basic Loop Trouble - need to convert active sheet to integer
    By mick86 in forum Excel General
    Replies: 9
    Last Post: 09-18-2012, 02:50 PM
  5. Dim integer in For loop does not work, why?
    By 123wc in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-30-2011, 12:48 PM
  6. Replies: 3
    Last Post: 10-27-2011, 05:20 PM
  7. Count the number of occurrences of an integer withing a larger integer
    By nnktran in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2010, 01:04 PM

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