• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Vba code not running & required changes

paradise

Member
Dear Sir,
As enclosed in attachment,I did not get result in Column I,perhaps there is some sort of error in vba code while running ,kindly have a look into it and resolve the issue,it would be a great help to me.The vba code is as follows with file enclosed in attachment.


Code:
Sub LIFO()
Dim a As Variant, Cost As Double, sumIn As Double, sumOut As Double, _
  i As Long, ii As Long
With Application
  .ScreenUpdating = False
  .Calculation = xlCalculationManual
End With
With Sheets("LIFO")
  .Range("i7", .Cells(Rows.Count, "i").End(xlUp)).ClearContents
  a = .Range("e7", .Cells(Rows.Count, "g").End(xlUp)).Resize(, 5).Value
  For i = LBound(a, 1) To UBound(a, 1)
  If Not IsEmpty(a(i, 3)) Then
  sumOut = a(i, 3)
  For ii = i - 1 To 1 Step -1
  If Not IsEmpty(a(ii, 2)) Then
  sumIn = sumIn + a(ii, 2)
  If sumIn > sumOut Then
  Exit For
  Else
  Cost = Cost + a(ii, 1) * a(ii, 2)
  a(ii, 2) = Empty
  End If
  End If
  Next
  If sumIn - sumOut > 0 Then
  Cost = (Cost + (a(ii, 1) * (a(ii, 2) - (sumIn - sumOut)))) / sumOut
  a(ii, 2) = sumIn - sumOut
  Else
  Cost = Cost / sumOut
  End If
  a(i, 5) = Cost
  sumIn = 0: sumOut = 0: Cost = 0: n = ii
  End If
  Next
  .Range("i7").Resize(UBound(a, 1)) = Application.Index(a, , 5)
  Erase a
End With
With Application
  .ScreenUpdating = True
  .Calculation = xlCalculationAutomatic
End With
Erase a
End Sub

Sub AVR_COST()
Dim a, i As Long, Bal As Double, Debit As Double
Dim AVcost As Double
With Application
  .Calculation = xlCalculationManual
  .EnableEvents = False
End With
With Sheets("AVR COST")
  a = .Range("e7", .Cells(.Rows.Count, "g").End(xlUp)).Resize(, 3).Value
  .Range("i7", .Cells(.Rows.Count, "i").End(xlUp)).ClearContents
  ReDim Preserve a(1 To UBound(a, 1), 1 To 4)
  For i = LBound(a, 1) To UBound(a, 1)
  If a(i, 2) > 0 Then
  Bal = Bal + a(i, 2)
  Debit = Debit + a(i, 1) * a(i, 2)
  AVcost = Debit / Bal
  ElseIf a(i, 3) > 0 Then
  a(i, 4) = AVcost
  Debit = Debit - a(i, 3) * AVcost
  Bal = Bal - a(i, 3)
  End If
  Next
  .Range("i7").Resize(UBound(a, 1)) = Application.Index(a, 0, 4)
  Erase a
End With
With Application
  .Calculation = xlCalculationAutomatic
  .EnableEvents = True
End With
End Sub
Thanks in advance,

With Best Rgds,
 

Attachments

  • DATA.xlsm
    21.7 KB · Views: 2
Last edited by a moderator:
Hi,

without any clear explaination (Any error message ? Line ? What's the purpose ? And so on …),
best is to follow your code by yourself using F8 key to check variables values and what's going on …

Regards.​
 
Thanks for the reply.

My main purposes is to get the outcome into Column I in the above enclosed workbook when the macro is executed.Since I don't know vba, hence I cannot modify the code,hence an expert of vba code can solve the problem .The said code I found in the somewhere in the website.

With Best Rgds,
 
Back
Top