Bug in VBA?

  • Hello,
    I noticed that the counter below only works in some cases. In these cases below, for example, 1.3 works, 1.4 goes wrong...


    Ok:
    For Var = 0.1 To 1.3 Step 0.1
    Range("A1") = Var
    Next


    Error:
    For Var = 0.1 To 1.4 Step 0.1
    Range("A1") = Var
    Next


    Madness! :duh:

  • Hi
    I think you have to try in this way


    Bye,bye
    Mario

  • It's a floating point issue. It's impossible to exactly represent some numbers in binary so you get small differences if you keep adding them. You can see it happening if you format A1 to show lots of decimal places and then use code like this:


    Code
    Range("A1") = 1.4 - var


    instead of the

    Code
    Range("A1") = var


    line.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Not the only way. You can just loop from 1 to 14 and divide the loop value by 10 when using it.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!