Floating Point Errors in Excel VBA

I recently had a project where I had to total dollar figures in two columns and compare that total to the reported total in a third. Easy, right?

The VBA code was pretty simple:

If Cells(ActiveCell.Row, 4).Value + Cells(ActiveCell.Row, 6).Value <> _
     Cells(ActiveCell.Row, 8).Value Then
        Cells(ActiveCell.Row, 10).Value = “Totals don’t match”
        '(e.g. if D2 + F2 <> H2 Then J2 contains an error message)
End If

In a set of 70 rows, I kept getting two rows where the totals were equal, but reported as not equal.

I tested the total and match in a separate cell. Correct answer.

I tested the total and match in VBA and got the wrong answer.

I checked for stray decimals. I checked for numbers as strings. I checked for hidden values. I rounded each number in code. I found one column had Wrapped Text and fixed that. Still, all wrong answers.

I copied/pasted values only to remove any formatting issues. Wrong answer.

I deleted the numbers and inserted two other numbers and their total. Correct answer.

I retyped the actual numbers and ran the code. Wrong answer.

I found several articles about precision issues. (https://blogs.office.com/en-us/2008/04/10/understanding-floating-point-precision-aka-why-does-excel-give-me-seemingly-wrong-answers/) but my numbers were way smaller. I remembered how you had to round numbers without a Round function: Multiply by 100 to turn into an integer, then divide by 100 to get the original value.

I changed the code to convert the numbers to long integers and tested the long integers. (They had to be Longs not Integers due to the 32,767 limit on Integer values.) Success.

Dim lngFirst, lngSecond, lngTotal as Long

lngFirst =  Cells(ActiveCell.Row, 4).Value * 100
lngSecond = Cells(ActiveCell.Row, 6).Value * 100
lngTotal = Cells(ActiveCell.Row, 8).Value * 100

If lngFirst + lngSecond <> lngTotal Then
   Cells(ActiveCell.Row, 10).Value = “Totals don’t match”
End If

If you want to try it, these numbers contain the floating point error. Note that this will not give an error in Excel, only in VBA code. The first row is the Column Letters.

D E F G H I J
1407.94 1.66 1409.60 “Error Message”

Comments

Floating Point Errors in Excel VBA — 2 Comments

  1. Hello,
    I am trying to maintain an inventory of items. i have…
    Col 1 is onhand qty
    Col 2 is qty being removed/used from Col 1
    Col 3 is new onhand qty.
    Heres the problem. The onhand qty needs to reduce each time an item is used, but this seems to create a circular ref. where the result relies on itself. When i take col1 and subtract col2 i get col3. Thats fine. But for the next time, i need to use the NEW onhand qty as the inventory depletes as its used. I can do this in code, no problem. But i am trying to do this simple thing with formulas. The reason is i want this list to be able to be emailed to a mobile device, but the mobile versions of Excel will not run code. Only formulas. Can you assist in doing this with using only formulas? I have searched in vain. Thank you ahead

    • Can’t be done. Formulas do not change other cell’s values, only code. So if A2 = 12 and B2 = 10, then C2 will = 2. To then update A2 with the new value requires an action, not a formula, which is what code does. Can you create a new row for each update? That way, A2 = 12, B2=10, and both C2 and A3 = 2. Then, your next update, B3, will subtract from A3, with the result going in C3 and A4. And so on.

Leave a Reply

Your email address will not be published. Required fields are marked *