Hi
This is my first posting; so please excuse me if I'm not doing it right.
I found the below VBA code, written by someone in Australia. I like this VBA code as it works real well based on the author's design. It computes the Cost of Goods sold (COGS) based on three (3) criteria: 1) Month, 2) Product, and 3) Quantity Sold. It appears that by specifying the “month” criteria, along with the other two criteria, it computes all products sold for that “month” regardless of the number of years in which that month occurs. In other words, my understanding of it in layman term is as follows: Compute all COGS in the month(s) of July only whether or not goods were sold in say, July 2015, July 2016, and July 2017. So, if 20 goods were sold in July 2015, 50 goods were sold in July 2016; and 70 goods were sold in July 2017, the code will compute the COGS for quantity 20, 50, and 70.
I don’t know much about VBA and would like and appreciate some help in modifying the below VBA code to do the following:
1. Compute the COGS between two specified dates
So, it will operate as follows:
If “From_date” => “date1” and “To_date” <= “date2” compute the COGS between those two specified spreadsheet dates, “date1” and “date2”
“date2’ will have to be a new criteria in my Spreadsheet in addition of the other 3 criteria.
Thanks a million in advance to any or all who may respond. Please attach your Spreadsheet, showing your Code and sample data used
This is my first posting; so please excuse me if I'm not doing it right.
I found the below VBA code, written by someone in Australia. I like this VBA code as it works real well based on the author's design. It computes the Cost of Goods sold (COGS) based on three (3) criteria: 1) Month, 2) Product, and 3) Quantity Sold. It appears that by specifying the “month” criteria, along with the other two criteria, it computes all products sold for that “month” regardless of the number of years in which that month occurs. In other words, my understanding of it in layman term is as follows: Compute all COGS in the month(s) of July only whether or not goods were sold in say, July 2015, July 2016, and July 2017. So, if 20 goods were sold in July 2015, 50 goods were sold in July 2016; and 70 goods were sold in July 2017, the code will compute the COGS for quantity 20, 50, and 70.
I don’t know much about VBA and would like and appreciate some help in modifying the below VBA code to do the following:
1. Compute the COGS between two specified dates
So, it will operate as follows:
If “From_date” => “date1” and “To_date” <= “date2” compute the COGS between those two specified spreadsheet dates, “date1” and “date2”
“date2’ will have to be a new criteria in my Spreadsheet in addition of the other 3 criteria.
Code:
Function FIFO2(ByRef Data, ByVal Dte As Long, ByVal Product As String, ByVal Stock As Double) As Double
Dim ar As Variant
Dim i As Long
Const DateCol As Long = 1
Const ProdCol As Long = 2
Const QtyCol As Long = 3
Const CostCol As Long = 4
ar = Data
For i = LBound(ar, 1) To UBound(ar, 1)
If Month(ar(i, DateCol)) = Month(Dte) Then
If ar(i, ProdCol) = Product Then
If Stock < ar(i, QtyCol) Then
FIFO2 = FIFO2 + Stock * ar(i, CostCol)
Exit Function
Else
FIFO2 = FIFO2 + (ar(i, QtyCol) * ar(i, CostCol))
Stock = Stock - ar(i, QtyCol)
If Stock <= 0 Then Exit Function
End If
End If
End If
Next
End Function
Thanks a million in advance to any or all who may respond. Please attach your Spreadsheet, showing your Code and sample data used
Last edited by a moderator: