How to assign unit cost to each sales base on FIFO

solau

New Member
Joined
Feb 7, 2016
Messages
12
Hi Dudes,

I am having this VBA task and i am facing extreme difficulties. It will be great if I can receive any suggestions/ inspiring solutions to this problem.

Basically, there are three worksheets in my excel workbook.

First,it is the purchase worksheet where the columns are as below. (I have like 10 different products, but to simplify the situation, lets assume there is only product A)

DateProductQuantityCost
01-Jan-16Product A5100
02-Jan-16Product A10130
03-Jan-16Product A1590
04-Jan-16Product A25105

<colgroup><col><col span="3"></colgroup><tbody>
</tbody>

Second, it is the sales worksheet where the columns are as below.
DateProductQuantityPriceTotal amount
03-Jan-16Product A3320960
04-Jan-16Product A23201280
05-Jan-16Product A4320960

<tbody>
</tbody>


Third, it is the PNL sheet which extracts information from the first and second sheet and calculate the daily PNL on product A. This is where I will need the VBA codes, I am fine with the part to extract the information from first and second sheet. However, I am not able to assign costs to.

The below is the ideal look. It will extract all the sales from sheet 2 and try to assign the unit cost (column 5) base on purchase from sheet 1. The question is how i can get its unit cost (column 5) from sheet 1 base on FIFO. :ROFLMAO:

For example, the sales on 3 Jan should have a unit cost of 100 because it is from the first purchase and so is the sales on 4 Jan.

For the sales on 5 Jan, since the first purchase are all sold, its unit cost will be from the second purchase on 2 Jan and it will be 130.

DateProductQuantityUnit PriceUnit CostUnit ProfitTotal Profit
03-Jan-16Product A3320100220660
04-Jan-16Product A2320100220440
05-Jan-16Product A4320130190760

<tbody>
</tbody>


Thank you very much for the kind help!
 
ha Mick, this is not more robust, the calculation results in Sheet 3 (E, F , G ) are all wrong haha.

But I have amended it and it is working fine.

Thanks so much showing all the arrays and it helps improve my understanding of what you are doing there !

Coding is really fun and sometimes, I just need to think out of the box.

There is no certain answer and diff ppl can have diff solutions to a specific problems .

Thanks so much !!!!
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi Mick,

From the codes, I am having some questions on array.

The below is for copying cells value into the array for later manipulation in the purchase sheet (the one which records costs of the purchase).

<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}o\:* {behavior:url(#default#VML);}x\:* {behavior:url(#default#VML);}.shape {behavior:url(#default#VML);}</style><![endif]-->
DateProductQuantityCost
01-Jan Product A4100
02-Jan Product B2130

<!--StartFragment--> <colgroup><col width="65" span="4" style="width:65pt"> </colgroup><tbody>
<!--EndFragment--></tbody>

The below codes are from you.


-------From Mick,
Dim i, ii, c As Integer
Dim stup()


c = 0
For Each prod In Range(Cells(2, 2), Cells(3, 2))


For i = 1 To prod.Offset(, 1)
c = c + 1
ReDim Preserve stup(1 To 4, 1 To c)
stup(1, c) = prod.Offset(, -1)
stup(2, c) = prod
stup(3, c) = prod.Offset(, 1)
stup(4, c) = prod.Offset(, 2)
Next i


Next prod

------ from Mick

When we look at the cells(x,y), x is the row index and y is the column index

Hence, I think it will be more direct if I code like the below. Also, it will not require the use of transpose when I later want to put the array value into the sheets.

Sadly, I will receive an error saying that "Subscript out of range".

I have been searching around and still cannot get an answer to fix my codes.

Can you please provide some insights?

----from Harris
Dim i, ii, c As Integer
Dim stup()


c = 0
For Each prod In Range(Cells(2, 2), Cells(3, 2))


For i = 1 To prod.Offset(, 1)
c = c + 1
ReDim Preserve stup(1 To c, 1 To 4)
stup(c, 1) = prod.Offset(, -1)
stup(c, 2) = prod
stup(c, 3) = prod.Offset(, 1)
stup(c, 4) = prod.Offset(, 2)
Next i


Next prod
-----from Harris
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,963
Messages
6,127,951
Members
449,412
Latest member
montand

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