Allocating Receipt amount with invoices based on fifo method

amandeep08

Board Regular
Joined
Mar 20, 2011
Messages
130
Office Version
  1. 365
Hi,

In my workbook I have 2 sheets - 1st sheet has the case and 2nd has the solution of how the case should look like after running the macro.

In sheet - "CASE" I have 2 sets of data namely Data 1 - Invoices & Data 2 - Receipts.

I have a code which would allocate all the receipts with the Invoices serial no. wise.

If a single receipt is not sufficient to adjust a Invoice, a row is inserted and in that row the remaining balance of invoice is adjusted against next receipt.

The Macro is working fine if i am applying to one customer but how to apply on multiple customers in one go.

Sample (1).xlsm
ABCDEFGHI
1InvoiceReceipts
2S.No.CustomerInvoiceReceipt no.Receipt adjustedS.noReceiptCustomer
31A2865114325A
42A4298215757A
53A141253120A
64A95041433A
751433A
861433A
Case


Below is the code:

Sub Test()
Dim rng As Range, a, b, c(1 To 100000, 1 To 5), i As Long, j As Long, pb As Long, pc As Long, v3
With Sheets("Case ")
Set rng = .Range("A1").CurrentRegion
a = rng.Offset(2).Resize(, 3).Value
b = .Range("G3:H" & .Cells(.Rows.Count, "G").End(xlUp).Row).Value
pb = 1
For i = 1 To UBound(a, 1)
v3 = a(i, 3)
For j = pb To UBound(b, 1)
pc = pc + 1
c(pc, 1) = a(i, 1): c(pc, 2) = a(i, 2): c(pc, 3) = v3: c(pc, 4) = b(j, 1)
If a(i, 3) < b(j, 2) Then
c(pc, 5) = a(i, 3)
b(j, 2) = b(j, 2) - a(i, 3)
Exit For
ElseIf a(i, 3) = b(j, 2) Then
c(pc, 5) = a(i, 3)
pb = pb + 1
Exit For
Else
c(pc, 5) = b(j, 2)
a(i, 3) = a(i, 3) - b(j, 2)
pb = pb + 1
End If
Next j
If pb > UBound(b, 1) Then
pc = pc + 1
c(pc, 1) = a(i, 1): c(pc, 2) = a(i, 2): c(pc, 3) = v3
End If
Next i
End With
With Sheets.Add(after:=Sheets(Sheets.Count))
rng.Rows("1:2").Copy .Range("A1")
.Range("A3").Resize(pc, UBound(c, 2)).Value = c
With .Range("A1").CurrentRegion
.Borders.Weight = xlThin
.EntireColumn.AutoFit
End With
End With
End Sub



Thanks in advance!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,215,232
Messages
6,123,768
Members
449,122
Latest member
sampak88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top