+ Reply to Thread
Results 1 to 5 of 5

For Loop Skipping last value for certain range of values

  1. #1
    Registered User
    Join Date
    01-24-2020
    Location
    Dubai, UAE
    MS-Off Ver
    2016 Excel
    Posts
    3

    For Loop Skipping last value for certain range of values

    Hi, I wrote the following code using a for loop:

    Please Login or Register  to view this content.
    This is supposed to output values from 0 to 1.5 in column A at 0.1 intervals.

    The issue I'm facing is that for values upto t=1.3, the last output value is 1.3, However if I put t=1.4 it still gives the last value as 1.3

    if t=1.5, last output value is 1.4! for t=1.6 last output is 1.7 and so on...

    This continues till t=4.7, after which for t=4.8 last value is also 4.8


    Any suggestions on what's going on?
    Last edited by Leith Ross; 01-25-2020 at 01:56 PM. Reason: Added Code Tags

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,827

    Re: For Loop Skipping last value for certain range of values

    Repeatedly adding 0.1 is one of the classic ways (going all the way back to the '70s and '80s) of illustrating floating point errors (probably more than you want to know in these links: https://www.excelforum.com/groups/ma...nd-errors.html ). When VBA comes to the next statement, it adds step to the current value, tests if that is less than or equal to upper limit, and only executes the loop again if the result of the boolean test is true. If the upper limit is 1.4, and the 1.3+0.1 addition results in 1.40000000001, then the test is false and the loop terminates without executing the i=1.4 iteration.

    Solutions involve figuring out how to deal with the floating point error or avoid it altogether.

    To avoid the issue, I might modify the code to work with integers instead. For i=1 to 14 step 1: debug.print i/10: next i
    One could add a small error value to t so that floating point error will not cause the loop to terminate early. For i=1 to t+0.05 step 0.1: debug.print i: next i
    One could use a Do...Loop where you can use a Round function and your own boolean test to better control loop termination i=0:Do Until Round(i,1)>t: debug.print i: i=i+1: Loop

    or any other strategy that strikes your fancy.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: For Loop Skipping last value for certain range of values

    Quote Originally Posted by mahahkhan View Post
    Any suggestions on what's going on?
    Hi, just try Currency data type rather than Double obviously …

  4. #4
    Registered User
    Join Date
    01-24-2020
    Location
    Dubai, UAE
    MS-Off Ver
    2016 Excel
    Posts
    3
    Thanks a lot for the detailed reply and the link. Really appreciate it!😊

  5. #5
    Registered User
    Join Date
    01-24-2020
    Location
    Dubai, UAE
    MS-Off Ver
    2016 Excel
    Posts
    3
    Thanks a bunch!

+ 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] Loop is skipping iterations
    By Jovica in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-22-2014, 06:50 AM
  2. [SOLVED] Skipping over certain sheets in a For Each loop
    By NicholasL in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-12-2013, 03:15 PM
  3. Skipping Loop
    By yasar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-22-2013, 06:15 AM
  4. Skipping first row in a Loop
    By johnph in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-05-2012, 11:42 AM
  5. Replies: 11
    Last Post: 05-31-2009, 05:41 PM
  6. [SOLVED] Loop while skipping blanks
    By jhahes in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-22-2006, 04:50 PM
  7. Skipping cells in a Do Until loop
    By medicenpringles in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-28-2005, 11:42 AM
  8. Do loop skipping
    By medicenpringles in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2005, 10:05 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