Tôi có tìm được công thức tính FIFO trên trang www.mrexcel.com. Nhưng khi chạy nó cứ báo lỗi debug dòng 13 từ trên xuống "sumout =(a,3)", các bạn chỉ giúp lỗi ở đâu.
Số liệu bắt đầu từ dòng 7, cột A: TT, cột B: Date, cột C: Hàng hoá, cột D: IN or OUT, cột E: ĐG mua, cột F: SL mua, cột G: SL bán, cột H: Balance, cột I: ĐG FIFO (Sheet phải là FIFO)
Sub FIFO()
Dim a As Variant, Cost As Double, sumIn As Double, sumOut As Double, _
i As Long, ii As Long, n As Long
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With Sheets("FIFO")
.Range("i7", .Cells(Rows.Count, "i").End(xlUp)).ClearContents
a = .Range("e7", .Cells(Rows.Count, "g").End(xlUp)).Resize(, 5).Value
n = 1
For i = LBound(a, 1) To UBound(a, 1)
If Not IsEmpty(a(i, 3)) Then
sumOut = a(i, 3)
For ii = n To i - 1
If Not IsEmpty(a(ii, 2)) Then
sumIn = sumIn + a(ii, 2)
If sumIn > sumOut Then
Exit For
Else
Cost = Cost + a(ii, 1) * a(ii, 2)
a(ii, 2) = Empty
End If
End If
Next
If sumIn - sumOut > 0 Then
Cost = (Cost + (a(ii, 1) * (a(ii, 2) - (sumIn - sumOut)))) / sumOut
a(ii, 2) = sumIn - sumOut
Else
Cost = Cost / sumOut
End If
a(i, 5) = Cost
sumIn = 0: sumOut = 0: Cost = 0: n = ii
End If
Next
.Range("i7").Resize(UBound(a, 1)) = Application.Index(a, 0, 5)
Erase a
End With
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
Nếu có thể bạn giải thích hộ từng bước nó làm thì tốt quá. Tôi mới tập toẹ vào VBA. Xin cám ơn
Số liệu bắt đầu từ dòng 7, cột A: TT, cột B: Date, cột C: Hàng hoá, cột D: IN or OUT, cột E: ĐG mua, cột F: SL mua, cột G: SL bán, cột H: Balance, cột I: ĐG FIFO (Sheet phải là FIFO)
Sub FIFO()
Dim a As Variant, Cost As Double, sumIn As Double, sumOut As Double, _
i As Long, ii As Long, n As Long
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With Sheets("FIFO")
.Range("i7", .Cells(Rows.Count, "i").End(xlUp)).ClearContents
a = .Range("e7", .Cells(Rows.Count, "g").End(xlUp)).Resize(, 5).Value
n = 1
For i = LBound(a, 1) To UBound(a, 1)
If Not IsEmpty(a(i, 3)) Then
sumOut = a(i, 3)
For ii = n To i - 1
If Not IsEmpty(a(ii, 2)) Then
sumIn = sumIn + a(ii, 2)
If sumIn > sumOut Then
Exit For
Else
Cost = Cost + a(ii, 1) * a(ii, 2)
a(ii, 2) = Empty
End If
End If
Next
If sumIn - sumOut > 0 Then
Cost = (Cost + (a(ii, 1) * (a(ii, 2) - (sumIn - sumOut)))) / sumOut
a(ii, 2) = sumIn - sumOut
Else
Cost = Cost / sumOut
End If
a(i, 5) = Cost
sumIn = 0: sumOut = 0: Cost = 0: n = ii
End If
Next
.Range("i7").Resize(UBound(a, 1)) = Application.Index(a, 0, 5)
Erase a
End With
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
Nếu có thể bạn giải thích hộ từng bước nó làm thì tốt quá. Tôi mới tập toẹ vào VBA. Xin cám ơn