Hi
check this attachment..maintain data as per two sheet & click on blue button to run code:
updated code is:
Sub fifoadjustment()
Dim i As Integer, j As Integer, customer As String
Dim payment_bal As Long, invoice_bal As Long, invoice_amt As Long
Dim assigned As Long
payment_bal = 0
invoice_bal = 0
invoice_amt = 0
Sheets("Payments").Range("E2:E50000").Value = ""
Sheets("Invoice").Range("E2:E50000").Value = ""
For i = 2 To Sheets("Invoice").Range("A55000").End(xlUp).Row
invoice_amt = Sheets("Invoice").Cells(i, 2).Value
customer = Sheets("Invoice").Cells(i, 3).Value
invoice_bal = 0
payment_bal = 0
assigned = 0
For j = 2 To Sheets("Payments").Range("A55000").End(xlUp).Row
If Sheets("Payments").Cells(j, 3).Value = customer Then
If Sheets("Payments").Cells(j, 5).Value = "" Then
If invoice_bal = 0 Then
If Sheets("Payments").Cells(j, 2).Value < invoice_amt Then
invoice_bal = invoice_amt - Sheets("Payments").Cells(j, 2).Value
payment_bal = 0: Sheets("Payments").Cells(j, 5).Value = 0
End If
Else
assigned = Application.WorksheetFunction.Min(Sheets("Payments").Cells(j, 2).Value, invoice_bal)
invoice_bal = invoice_bal - assigned 'Application.WorksheetFunction.Min(Sheets("Payments").Cells(j, 2).Value, invoice_bal)
End If
If invoice_bal = 0 Then
Sheets("Invoice").Cells(i, 5).Value = Sheets("Payments").Cells(j, 1).Value
If assigned = 0 Then
payment_bal = Sheets("Payments").Cells(j, 2).Value - invoice_amt
Else
payment_bal = Sheets("Payments").Cells(j, 2).Value - assigned
End If
Sheets("Payments").Cells(j, 5).Value = payment_bal 'Sheets("Payments").Cells(j, 5).Value - assigned
Else
Sheets("Invoice").Cells(i, 5).Value = "Amt recieved " & invoice_amt - invoice_bal
End If
Else
If Sheets("Payments").Cells(j, 5).Value <> 0 Then
If invoice_bal = 0 Then
If Sheets("Payments").Cells(j, 5).Value < invoice_amt Then
invoice_bal = invoice_amt - Sheets("Payments").Cells(j, 5).Value
payment_bal = 0: Sheets("Payments").Cells(j, 5).Value = 0
End If
Else
assigned = Application.WorksheetFunction.Min(Sheets("Payments").Cells(j, 5).Value, invoice_bal)
invoice_bal = invoice_bal - Application.WorksheetFunction.Min(Sheets("Payments").Cells(j, 2).Value, invoice_bal)
End If
If invoice_bal = 0 Then
Sheets("Invoice").Cells(i, 5).Value = Sheets("Payments").Cells(j, 1).Value
If assigned = 0 Then
payment_bal = Sheets("Payments").Cells(j, 2).Value - invoice_amt
Else
payment_bal = Sheets("Payments").Cells(j, 2).Value - assigned
End If
Sheets("Payments").Cells(j, 5).Value = payment_bal 'Sheets("Payments").Cells(j, 5).Value - assigned
Else
Sheets("Invoice").Cells(i, 5).Value = "Amt recieved " & invoice_amt - invoice_bal
End If
End If
End If
End If
Next j
Next i
End Sub
Cheers!!
Bookmarks