Consulting

Results 1 to 7 of 7

Thread: Problem with decimals in VBA code

  1. #1
    VBAX Regular
    Joined
    Aug 2004
    Posts
    22
    Location

    Problem with decimals in VBA code

    I frequently have problems with decimals when I perform calculations with VBA.
    To illustrate the problem I just ran the following:

    a= 27.45
    b= 27.00
    c= a -b

    and c is not 0.45 but 0.4499999999.

    As you probably understand already, I'm just a novice at this, so I'm sure there's an easy and obvious way around this. but I'll be overjoyed if anyone can enlighten me..

    Thanks in advance !

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Excel has trouble with numbers, what can I say. You can force the precission that you want though.

    c = Round(a - b, 2)

  3. #3
    VBAX Regular
    Joined
    Aug 2004
    Posts
    22
    Location
    Thanks, that's so obvious that I should have thought of it myself. My problem is really that when I use numbers as conditions in loops or comparisons, for example

    Do While a > b
    even if I have rounded off a and b to 2 decimals in advance of the Do While statement, I experience incorrect results because of the (lack of) rounding off..

    Johannes

  4. #4

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You can try something like:
    [vba]

    Do

    'Code

    a = some number
    b = some number
    c = Round(a,2)
    d = Round(b,2)

    'More Code

    Loop While c > d
    [/vba]

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi,
    You could also try

    Do While a > b - 0.01 (or b + 0.01) as appropriate

    MD

  7. #7
    VBAX Regular
    Joined
    Aug 2004
    Posts
    22
    Location
    Thanks guys. I'm not convinced that I'm covered in all situations, but at least I have a few more ideas about how to handle this situation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •