Help with calculating cost of goods

Baziwan

New Member
Joined
Sep 4, 2018
Messages
32
[TABLE="width: 804"]
<tbody>[TR]
[TD="width: 89, bgcolor: transparent"]Order No
[/TD]
[TD="width: 89, bgcolor: transparent"]Account No
[/TD]
[TD="width: 89, bgcolor: transparent"]Name
[/TD]
[TD="width: 89, bgcolor: transparent"]Stock Code
[/TD]
[TD="width: 89, bgcolor: transparent"]Description
[/TD]
[TD="width: 89, bgcolor: transparent"]Quantity
[/TD]
[TD="width: 89, bgcolor: transparent"]Unit Cost
[/TD]
[TD="width: 89, bgcolor: transparent"]Net Amount
[/TD]
[TD="width: 89, bgcolor: transparent"]Tax Amount
[/TD]
[TD="width: 89, bgcolor: transparent"]Gross Amount
[/TD]
[TD="width: 89, bgcolor: transparent"]Order Date
[/TD]
[TD="width: 89, bgcolor: transparent"]Remaining Stock
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]7
[/TD]
[TD="bgcolor: transparent"]22
[/TD]
[TD="bgcolor: transparent"]Ex1
[/TD]
[TD="bgcolor: transparent"]APPLE1
[/TD]
[TD="bgcolor: transparent"]Apples
[/TD]
[TD="bgcolor: transparent"]500
[/TD]
[TD="bgcolor: transparent"]£0.50
[/TD]
[TD="bgcolor: transparent"]£12.50
[/TD]
[TD="bgcolor: transparent"]£2.50
[/TD]
[TD="bgcolor: transparent"]£15.00
[/TD]
[TD="bgcolor: transparent"]10/07/2018
[/TD]
[TD="bgcolor: transparent"]175
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]5
[/TD]
[TD="bgcolor: transparent"]38
[/TD]
[TD="bgcolor: transparent"]Ex7
[/TD]
[TD="bgcolor: transparent"]PEAR1
[/TD]
[TD="bgcolor: transparent"]Pears
[/TD]
[TD="bgcolor: transparent"]200
[/TD]
[TD="bgcolor: transparent"]£1.50
[/TD]
[TD="bgcolor: transparent"]£37.50
[/TD]
[TD="bgcolor: transparent"]£7.50
[/TD]
[TD="bgcolor: transparent"]£45.00
[/TD]
[TD="bgcolor: transparent"]17/07/2018
[/TD]
[TD="bgcolor: transparent"]75
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]6
[/TD]
[TD="bgcolor: transparent"]22
[/TD]
[TD="bgcolor: transparent"]Ex1
[/TD]
[TD="bgcolor: transparent"]APPLE1
[/TD]
[TD="bgcolor: transparent"]Apples
[/TD]
[TD="bgcolor: transparent"]250
[/TD]
[TD="bgcolor: transparent"]£1.00
[/TD]
[TD="bgcolor: transparent"]£250.00
[/TD]
[TD="bgcolor: transparent"]£50.00
[/TD]
[TD="bgcolor: transparent"]£300.00
[/TD]
[TD="bgcolor: transparent"]17/07/2018
[/TD]
[TD="bgcolor: transparent"]250
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]3
[/TD]
[TD="bgcolor: transparent"]41
[/TD]
[TD="bgcolor: transparent"]Ex2
[/TD]
[TD="bgcolor: transparent"]ORANG/1
[/TD]
[TD="bgcolor: transparent"]Oranges
[/TD]
[TD="bgcolor: transparent"]180
[/TD]
[TD="bgcolor: transparent"]£3.00
[/TD]
[TD="bgcolor: transparent"]£540.00
[/TD]
[TD="bgcolor: transparent"]£108.00
[/TD]
[TD="bgcolor: transparent"]£648.00
[/TD]
[TD="bgcolor: transparent"]15/08/2018
[/TD]
[TD="bgcolor: transparent"]100
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]4
[/TD]
[TD="bgcolor: transparent"]52
[/TD]
[TD="bgcolor: transparent"]Ex8
[/TD]
[TD="bgcolor: transparent"]LEMON1
[/TD]
[TD="bgcolor: transparent"]Lemons
[/TD]
[TD="bgcolor: transparent"]100
[/TD]
[TD="bgcolor: transparent"]£2.00
[/TD]
[TD="bgcolor: transparent"]£200.00
[/TD]
[TD="bgcolor: transparent"]£40.00
[/TD]
[TD="bgcolor: transparent"]£240.00
[/TD]
[TD="bgcolor: transparent"]15/08/2018
[/TD]
[TD="bgcolor: transparent"]80
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]2
[/TD]
[TD="bgcolor: transparent"]38
[/TD]
[TD="bgcolor: transparent"]Ex7
[/TD]
[TD="bgcolor: transparent"]PEAR1
[/TD]
[TD="bgcolor: transparent"]Pears
[/TD]
[TD="bgcolor: transparent"]250
[/TD]
[TD="bgcolor: transparent"]£2.00
[/TD]
[TD="bgcolor: transparent"]£500.00
[/TD]
[TD="bgcolor: transparent"]£100.00
[/TD]
[TD="bgcolor: transparent"]£600.00
[/TD]
[TD="bgcolor: transparent"]18/08/2018
[/TD]
[TD="bgcolor: transparent"]250
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1
[/TD]
[TD="bgcolor: transparent"]22
[/TD]
[TD="bgcolor: transparent"]Ex1
[/TD]
[TD="bgcolor: transparent"]APPLE1
[/TD]
[TD="bgcolor: transparent"]Apples
[/TD]
[TD="bgcolor: transparent"]500
[/TD]
[TD="bgcolor: transparent"]£2.00
[/TD]
[TD="bgcolor: transparent"]£1,000.00
[/TD]
[TD="bgcolor: transparent"]£200.00
[/TD]
[TD="bgcolor: transparent"]£1,200.00
[/TD]
[TD="bgcolor: transparent"]30/09/2018
[/TD]
[TD="bgcolor: transparent"]500
[/TD]
[/TR]
</tbody>[/TABLE]



[TABLE="width: 806"]
<tbody>[TR]
[TD="width: 82, bgcolor: transparent"]Invoice No
[/TD]
[TD="width: 82, bgcolor: transparent"]Account Ref
[/TD]
[TD="width: 82, bgcolor: transparent"]Type
[/TD]
[TD="width: 82, bgcolor: transparent"]Name
[/TD]
[TD="width: 82, bgcolor: transparent"]Stock Code
[/TD]
[TD="width: 82, bgcolor: transparent"]Description
[/TD]
[TD="width: 82, bgcolor: transparent"]Quantity
[/TD]
[TD="width: 82, bgcolor: transparent"]Selling Price
[/TD]
[TD="width: 82, bgcolor: transparent"]Net Amount
[/TD]
[TD="width: 82, bgcolor: transparent"]Tax Amount
[/TD]
[TD="width: 82, bgcolor: transparent"]Gross Amount
[/TD]
[TD="width: 82, bgcolor: transparent"]Invoice Date
[/TD]
[TD="width: 82, bgcolor: transparent"]Cost of Goods Sold
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]8
[/TD]
[TD="bgcolor: transparent"]t1
[/TD]
[TD="bgcolor: transparent"]sale
[/TD]
[TD="bgcolor: transparent"]tom
[/TD]
[TD="bgcolor: transparent"]APPLE1
[/TD]
[TD="bgcolor: transparent"]Apples
[/TD]
[TD="bgcolor: transparent"]325
[/TD]
[TD="bgcolor: transparent"]£2.25
[/TD]
[TD="bgcolor: transparent"]£731.25
[/TD]
[TD="bgcolor: transparent"]£146.25
[/TD]
[TD="bgcolor: transparent"]£877.50
[/TD]
[TD="bgcolor: transparent"]26/09/2018
[/TD]
[TD="bgcolor: transparent"]£162.50
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]7
[/TD]
[TD="bgcolor: transparent"]T2
[/TD]
[TD="bgcolor: transparent"]sale
[/TD]
[TD="bgcolor: transparent"]Thomas
[/TD]
[TD="bgcolor: transparent"]APPLE1
[/TD]
[TD="bgcolor: transparent"]Apples
[/TD]
[TD="bgcolor: transparent"]300
[/TD]
[TD="bgcolor: transparent"]£2.50
[/TD]
[TD="bgcolor: transparent"]£750.00
[/TD]
[TD="bgcolor: transparent"]£150.00
[/TD]
[TD="bgcolor: transparent"]£900.00
[/TD]
[TD="bgcolor: transparent"]27/09/2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]6
[/TD]
[TD="bgcolor: transparent"]G1
[/TD]
[TD="bgcolor: transparent"]sale
[/TD]
[TD="bgcolor: transparent"]Gary
[/TD]
[TD="bgcolor: transparent"]PEAR1
[/TD]
[TD="bgcolor: transparent"]Pears
[/TD]
[TD="bgcolor: transparent"]25
[/TD]
[TD="bgcolor: transparent"]£4.00
[/TD]
[TD="bgcolor: transparent"]£100.00
[/TD]
[TD="bgcolor: transparent"]£20.00
[/TD]
[TD="bgcolor: transparent"]£120.00
[/TD]
[TD="bgcolor: transparent"]28/09/2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]5
[/TD]
[TD="bgcolor: transparent"]K1
[/TD]
[TD="bgcolor: transparent"]sale
[/TD]
[TD="bgcolor: transparent"]Keith
[/TD]
[TD="bgcolor: transparent"]APPLE1
[/TD]
[TD="bgcolor: transparent"]Apples
[/TD]
[TD="bgcolor: transparent"]50
[/TD]
[TD="bgcolor: transparent"]£3.00
[/TD]
[TD="bgcolor: transparent"]£150.00
[/TD]
[TD="bgcolor: transparent"]£30.00
[/TD]
[TD="bgcolor: transparent"]£180.00
[/TD]
[TD="bgcolor: transparent"]29/09/2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]9
[/TD]
[TD="bgcolor: transparent"]T2
[/TD]
[TD="bgcolor: transparent"]sale
[/TD]
[TD="bgcolor: transparent"]Thomas
[/TD]
[TD="bgcolor: transparent"]APPLE1
[/TD]
[TD="bgcolor: transparent"]Apples
[/TD]
[TD="bgcolor: transparent"]10
[/TD]
[TD="bgcolor: transparent"]£2.50
[/TD]
[TD="bgcolor: transparent"]£25.00
[/TD]
[TD="bgcolor: transparent"]£5.00
[/TD]
[TD="bgcolor: transparent"]£30.00
[/TD]
[TD="bgcolor: transparent"]30/09/2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]4
[/TD]
[TD="bgcolor: transparent"]D1
[/TD]
[TD="bgcolor: transparent"]sale
[/TD]
[TD="bgcolor: transparent"]Dot
[/TD]
[TD="bgcolor: transparent"]LEMON1
[/TD]
[TD="bgcolor: transparent"]Lemons
[/TD]
[TD="bgcolor: transparent"]10
[/TD]
[TD="bgcolor: transparent"]£3.00
[/TD]
[TD="bgcolor: transparent"]£30.00
[/TD]
[TD="bgcolor: transparent"]£6.00
[/TD]
[TD="bgcolor: transparent"]£36.00
[/TD]
[TD="bgcolor: transparent"]30/09/2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]3
[/TD]
[TD="bgcolor: transparent"]S2
[/TD]
[TD="bgcolor: transparent"]sale
[/TD]
[TD="bgcolor: transparent"]Sally
[/TD]
[TD="bgcolor: transparent"]PEAR1
[/TD]
[TD="bgcolor: transparent"]Pears
[/TD]
[TD="bgcolor: transparent"]50
[/TD]
[TD="bgcolor: transparent"]£4.00
[/TD]
[TD="bgcolor: transparent"]£200.00
[/TD]
[TD="bgcolor: transparent"]£40.00
[/TD]
[TD="bgcolor: transparent"]£240.00
[/TD]
[TD="bgcolor: transparent"]01/10/2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]2
[/TD]
[TD="bgcolor: transparent"]S1
[/TD]
[TD="bgcolor: transparent"]sale
[/TD]
[TD="bgcolor: transparent"]Simon
[/TD]
[TD="bgcolor: transparent"]APPLE1
[/TD]
[TD="bgcolor: transparent"]Apples
[/TD]
[TD="bgcolor: transparent"]25
[/TD]
[TD="bgcolor: transparent"]£3.00
[/TD]
[TD="bgcolor: transparent"]£75.00
[/TD]
[TD="bgcolor: transparent"]£15.00
[/TD]
[TD="bgcolor: transparent"]£90.00
[/TD]
[TD="bgcolor: transparent"]02/10/2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1
[/TD]
[TD="bgcolor: transparent"]T1
[/TD]
[TD="bgcolor: transparent"]sale
[/TD]
[TD="bgcolor: transparent"]Tom
[/TD]
[TD="bgcolor: transparent"]ORANG/1
[/TD]
[TD="bgcolor: transparent"]Oranges
[/TD]
[TD="bgcolor: transparent"]25
[/TD]
[TD="bgcolor: transparent"]£5.00
[/TD]
[TD="bgcolor: transparent"]£125.00
[/TD]
[TD="bgcolor: transparent"]£25.00
[/TD]
[TD="bgcolor: transparent"]£150.00
[/TD]
[TD="bgcolor: transparent"]03/10/2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
</tbody>[/TABLE]


Hi All,
Above is an example. There are 2 sheets, (Purchases) & (Sales). I've been attempting to get my macro
to calculate the cost price of a sale & update remaining stock but I keep hitting one stumbling block. Here's what I want to happen:
1. If the Cost of Goods Sold already has a value then go to next row.
2. If the Cost of Goods Sold is empty then .Find the stock code for the first purchase with remaining stock & calculate cost as follows
If the remaining stock is more than the qty sold then, (cost of goods sold = qty sold * unit cost) & the (remaining units = remaining units - qty sold)
If the remaining stock is less than the qty sold then, (cost of goods sold = remaining units * unit cost) & (qty = qty - remaining units) then
(remaining units = 0). Then .FindNext stockcode & (cost of goods sold = cost of goods sold + (qty * unit cost)). Then (remaining stock = remaining stock - qty)
Below is the code that I'm using but with my limited knowledge of Vba, whatever I try I just can't seem to get the cost prices into the correct cell
I really hope that you Excel Geniuses will be able to help me with this problem
Thanks in advance
ps, apologies if the code below requires some end ifs added/removed
Code:
Sub MatchSalesCostTest() 
    Dim NextRw As Long
    Dim rCl As Range
    Dim Rw As Long
    Dim Qty As Long
    Dim sFind As String
    Dim ws As Worksheet
    Dim ws2 As Worksheet
    
    Set ws = Worksheets("Purchases")
    Set ws2 = Worksheets("Sales")
    
        
    With Range("Sales")
        For Rw = 1 To .Rows.Count
            sFind = .Cells(Rw, 5).Text
            Qty = .Cells(Rw, 7).Value 'sales qty
            If Not IsEmpty(.Cells(Rw, 13)) Then GoTo DoneFinding 'if cell is filled, goes to donefinding
            
            With ws.Range("D1", ws.Cells(Rows.Count, "D").End(xlUp))
            Set rCl = .Find(sFind, LookIn:=xlValues, lookat:=xlWhole)
Previous:
            If rCl.Offset(0, 8).Value = 0 Then 'if remaining quantity is 0
            Set rCl = .FindNext(rCl)
            GoTo Previous
            End If
            If rCl.Offset(0, 8).Value < Qty Then 'if remaining qty is less than order qty
     ws2.Cells(Rw, 13).Value = rCl.Offset(0, 8).Value * rCl.Offset(0, 3).Value 'DOES NOT WORK
            Qty = Qty - rCl.Offset(0, 8).Value 
            rCl.Offset(0, 8).Value = 0 'remain qty = 0
            Set rCl = .FindNext(rCl)
     If rCl.Offset(0, 8).Value < Qty Then GoTo Previous 
            If rCl.Offset(0, 8).Value > Qty Then
            rCl.Offset(0, 8).Value = rCl.Offset(0, 8).Value - Qty
            ws2.Cells(Rw, 13).Value = ws2.Cells(Rw, 13).Value + (Qty * rCl.Offset(0, 3).Value) 'DOES NOT WORK
     GoTo DoneFinding
            End If
     If rCl.Offset(0, 8).Value > Qty Then
            rCl.Offset(0, 8).Value = rCl.Offset(0, 8).Value - Qty
            ws2.Cells(Rw, 13).Value = Qty * rCl.Offset(0, 3).Value 'DOES NOT WORK
            End If
DoneFinding:
     End With
        Next Rw
    End With
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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