Sub production_planning()
Dim sd As Date
Dim wh As Integer '-WORKING HOURS
Dim qt As Long 'QTY
Dim pc_hr As Integer '--PCS/HR
Dim hr_qt As Double 'NECESSARIES HOURS TO PRODUCT THE QTY = QT/PC_HR
Dim hr_qt_t As Double 'NECESSARIES HOURS TO PRODUCT THE QTY BUT NOT ON 10 HOURS, LESS.
Dim item As Range
Dim lastrow_item As Integer
Dim lastrow_plan As Integer
Dim n_items As Integer
Dim i As Integer
Dim wh_d As Double ' WORKING DAYS LEFT OF THE 10 PER DAY
i = 2
'--STARTING DATE
sd = ActiveSheet.Range("H2").Value
'--N ITEMS
lastrow_item = Cells(Rows.Count, "A").End(xlUp).Row
n_items = ActiveSheet.Range(("A") & lastrow_item).Value + 1
lastrow_plan = Cells(Rows.Count, "J").End(xlUp).Row
If lastrow_plan = 1 Then
Else
ActiveSheet.Range("J2:O" & lastrow_plan).ClearContents
End If
For Each item In ActiveSheet.Range(("B2:B") & n_items)
qt = item.Offset(0, 1).Value
pc_hr = item.Offset(0, 2).Value
hr_qt = qt / pc_hr
lastrow_plan = Cells(Rows.Count, "K").End(xlUp).Row
wh = item.Offset(0, 3).Value
'ActiveSheet.Range("N" & lastrow_plan + 1).Value = wh
Do While Not hr_qt <= 0
If ActiveSheet.Range("J2").Value = "" Then
Cells(i, 11).Value = item
Cells(i, 10).Value = sd
Cells(i, 12).Value = qt
Cells(i, 13).Value = pc_hr
Cells(i, 14).Value = wh
Cells(i, 15).Value = pc_hr * wh
wh_d = wh
hr_qt = hr_qt - Cells(i, 14).Value
Else
End If
If hr_qt < 0 Then
If i = 2 Then
hr_qt_t = Cells(i, 14).Value - (hr_qt * -1)
Else
hr_qt_t = Cells(i - 1, 14).Value - (hr_qt * -1)
End If
Cells(i + 1, 13).Value = pc_hr
Cells(i, 15).Value = Cells(i, 13).Value * Cells(i, 14).Value
Cells(i + 1, 12).Value = 0
Cells(i + 1, 11).Value = item
Cells(i + 1, 10).Value = Application.WorkDay(Cells(i, 10).Value, 1)
Cells(i + 1, 14).Value = hr_qt_t
hr_qt = hr_qt - Cells(i, 14).Value
ElseIf hr_qt = 0 Then
GoTo continueloop
ElseIf item <> Cells(i, 11).Value Then
Cells(i + 1, 11).Value = item
Cells(i + 1, 13).Value = pc_hr
If Cells(i, 14).Value < wh Then
If wh_d = 0 Then
Cells(i + 1, 10).Value = Application.WorkDay(Cells(i, 10).Value, 1)
Cells(i + 1, 14).Value = wh
wh_d = wh
Else
Cells(i + 1, 12).Value = qt
If (Cells(i + 1, 12).Value / pc_hr) > wh Then
Cells(i + 1, 14).Value = wh_d
ElseIf (Cells(i + 1, 12).Value / pc_hr) > wh_d Then
Cells(i + 1, 14).Value = wh_d
Else
Cells(i + 1, 14).Value = Cells(i + 1, 12).Value / pc_hr
End If
Cells(i + 1, 10).Value = Cells(i, 10).Value
wh_d = wh_d - Cells(i + 1, 14).Value
End If
ElseIf wh_d = 10 Then
Cells(i + 1, 10).Value = Application.WorkDay(Cells(i, 10).Value, 1)
Cells(i + 1, 14).Value = wh
wh_d = wh
End If
Cells(i + 1, 15).Value = Cells(i + 1, 13).Value * Cells(i + 1, 14).Value
Cells(i + 1, 12).Value = qt
hr_qt = hr_qt - Cells(i + 1, 14).Value
Else
Cells(i + 1, 13).Value = pc_hr
Cells(i, 15).Value = Cells(i, 13).Value * Cells(i, 14).Value
Cells(i + 1, 12).Value = Cells(i, 12).Value - Cells(i, 15)
Cells(i + 1, 11).Value = item
Cells(i + 1, 10).Value = Application.WorkDay(Cells(i, 10).Value, 1)
If (Cells(i + 1, 12).Value / pc_hr) > wh Then
Cells(i + 1, 14).Value = wh
Else
Cells(i + 1, 14).Value = Cells(i + 1, 12).Value / pc_hr
wh_d = wh
wh_d = wh_d - Cells(i + 1, 14).Value
End If
Cells(i + 1, 15).Value = Cells(i + 1, 13).Value * Cells(i + 1, 14).Value
hr_qt = hr_qt - Cells(i + 1, 14).Value
End If
i = i + 1
Loop
continueloop:
Next item
End Sub