Advertisement
hellohihi

Excel

Sep 27th, 2020
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function FIFO_PROFIT(SellPrice As Variant, SellQuantity As Variant, BuyPrice As Variant, BuyQuantity As Variant) As Variant
  2. 'Calculate the Profit according to the FIFO method
  3.  
  4.  
  5. '---------------Check Information for errors----------------------
  6.    SellSum = Application.WorksheetFunction.Sum(SellQuantity)
  7.     BuySum = Application.WorksheetFunction.Sum(BuyQuantity)
  8.    
  9.    
  10.     SellPCount = Application.WorksheetFunction.Count(SellPrice)
  11.     SellQCount = Application.WorksheetFunction.Count(SellQuantity)
  12.     BuyPCount = Application.WorksheetFunction.Count(BuyPrice)
  13.     BuyQCount = Application.WorksheetFunction.Count(BuyQuantity)
  14.  
  15.    
  16.     If SellSum > BuySum Then                                        'More sales than inventory, throw error
  17.        FIFO_PROFIT = VBA.CVErr(XlCVError.xlErrValue)
  18.     End If
  19.    
  20.     If (BuyPCount <> BuyQCount Or SellPCount <> SellQCount) Then    'Incomplete data, throw error
  21.        FIFO_PROFIT = VBA.CVErr(XlCVError.xlErrValue)
  22.     End If
  23. '-----------------------------------------------------------------
  24.  
  25. '--------------MoreVariables--------------------------------------
  26. Dim RunningSale As Variant
  27. Dim RunningBuy As Variant
  28. Dim RunningCost As Variant
  29. Dim RunningBuyQuantity As Variant
  30. Dim RunningSales As Variant
  31. Dim RunningProfit As Variant
  32. Dim Residual As Variant
  33. Dim UsedupResidual As Variant
  34. Dim y As Variant
  35.  
  36. y = 1
  37. RunningCost = 0
  38. Residual = 0
  39. UsedupResidual = 0
  40. RunningSales = 0
  41. RunningProfit = 0
  42. '-----------------------------------------------------------------
  43.  
  44.  
  45.     For x = 1 To SellQCount
  46.    
  47.     If y <> 1 Then                                                                  'BUGtest
  48.    
  49.         RunningBuyQuantity = Residual + BuyQuantity(y).Value2
  50.     End If
  51.             While (RunningBuyQuantity <= SellQuantity(x).Value2 And y <= BuyQCount) 'Bugtest
  52.                    
  53.                 If y = 1 Then
  54.                     RunningCost = RunningCost + (BuyPrice(y).Value2 * BuyQuantity(y).Value2)
  55.                 Else
  56.                     RunningCost = RunningCost + ((BuyPrice(y).Value2 * BuyQuantity(y).Value2) + (BuyPrice(y - 1).Value2 * Residual))
  57.                 End If
  58.                
  59.                 Residual = 0
  60.                 RunningBuyQuantity = RunningBuyQuantity + BuyQuantity(y).Value2
  61.                 y = y + 1
  62.                
  63.             Wend
  64.        
  65.         If RunningBuyQuantity > SellQuantity(x).Value2 Then
  66.             Residual = SellQuantity(x).Value2 - RunningBuyQuantity
  67.             UsedupResidual = BuyQuantity(y).Value2 - Residual
  68.             RunningCost = RunningCost + (UsedupResidual * BuyPrice(y).Value2)
  69.         End If
  70.        
  71.         RunningSales = SellPrice(x).Value2 * SellQuantity(x).Value2
  72.        
  73.         RunningProfit = RunningProfit + RunningSales - RunningCost
  74.        
  75.         RunningSales = 0
  76.         RunningCost = 0
  77.        
  78.     Next x
  79.    
  80.     FIFO_PROFIT = RunningProfit
  81.    
  82.  
  83.  
  84. End Function
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement