FractalWalk
Technical User
I am having trouble with the simplest of issues. I am using VBA in Excel 2010 to read a list of prices from a spreadsheet. I then summarize the array by categories from the spreadsheet to get subtotal prices by category. But when I add the prices together, VBA does not total the price correctly.
I am using a single data type and when it reads for example, 10.50, it might read it as 10.51132 or something odd like that. This throws the totals off. I have tried rounding and truncating decimal places but I can't find 100% foolproof way to add a list of 2 decimal numbers together accurately. The data in the spreadsheet is correct (no extra hidden numbers after the second decimal place).
Below are the relevant pieces of the code.
I am using a single data type and when it reads for example, 10.50, it might read it as 10.51132 or something odd like that. This throws the totals off. I have tried rounding and truncating decimal places but I can't find 100% foolproof way to add a list of 2 decimal numbers together accurately. The data in the spreadsheet is correct (no extra hidden numbers after the second decimal place).
Below are the relevant pieces of the code.
Code:
Sub test()
a = 2
Do Until Cells(a, 1) = Empty
ttl = WorksheetFunction.Round(ttl + Cells(a, 1), 2)
'doesn't work: ttl = ttl + Cells(a, 1)
'also doesn't work: ttl = ttl + Int(Cells(a, 1) * 100) / 100
a = a + 1
Loop
End Sub