如果数字有小数位,则Excel VBA计算错误

时间:2017-06-25 19:32:35

标签: excel vba fifo

我使用以下VBA代码进行FIFO计算。当我的数据中没有十进制数时,我的结果是正确的。但是当有十进制数字时,即使像25.12这样的小数字也是如此,我得到错误的结果。有人可以帮帮我吗?

Option Explicit

Sub FIFOCalc()
Dim sell As Long
Dim i As Integer
Dim j As Integer
Dim cnt As Long
Dim sale As Double
Dim ar As Variant
Dim Var As Variant

Range("G10:G1000").ClearContents
ar = Range("D10", Range("D65536").End(xlUp)) 'Purchase
Var = Range("E10", Range("E65536").End(xlUp)) 'Price

    For i = 10 To Range("B" & Rows.Count).End(xlUp).Row
        sale = 0
        sell = Range("F" & i)
        j = 1
        Do While sell > 0
            cnt = ar(j, 1)
            ar(j, 1) = IIf(ar(j, 1) > sell, ar(j, 1) - sell, 0) 'iif
            sell = sell - (cnt - ar(j, 1))
            sale = sale + (cnt - ar(j, 1)) * Var(j, 1)
            j = j + 1
        Loop
        Range("G65536").End(xlUp)(2) = sale 'output
    Next i
End Sub

0 个答案:

没有答案