LoganJBass
New Member
- Joined
- Jan 11, 2016
- Messages
- 1
Hello,
This is my first post, so please forgive me if I post any noob/dumb stuff with this post. I'm a bit stumped though with regard to writing this macro. The premise of the Macro is to simulate MRP within Excel. The first procedure is a simple calculation that takes the starting stock level and adds it to the requirement to its left for each row until there are no more requirements. That part works dandy, but will take any all suggestions to improve.
The 2nd sub is the part that is throwing me for a loop. Pun intended. So once I have my "Line of Balance" calculated, I want to find the first negative in Column H and insert a row before that negative value and then copy and paste a different row from worksheet2 that represent the PO. Once that line is now added to the "Line of Balance", I want my loop to do the calculation again find the next negative value and so on....The catch is, I only want to insert a row in sheet 1 if there are rows available in sheet 2 that contain a PO line. Once there are no more PO lines, I want a msgbox to appear and say no more POs or something and the end the loop. The code below doesnt seem to pick up both of these conditions and instead processes every negative before recalculating and wants to insert a row regardless if it is a PO row.
Again excuse the poor excuse of example below, couldn't figure out the screenshot insert. Noob....
Thank you!
Example of Sheet1
[TABLE="width: 782"]
<tbody>[TR]
[TD]Planned dates[/TD]
[TD]Needed Date after Changes[/TD]
[TD]MRP elemnt[/TD]
[TD]MRP elmnt data[/TD]
[TD]Rescheduling date[/TD]
[TD]Exception[/TD]
[TD]Rec./reqd qty[/TD]
[TD]Avail. quantity[/TD]
[TD]Stor. Loc.[/TD]
[TD]Model/Eff[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Stock[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]0.000[/TD]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1/1/2016[/TD]
[TD][/TD]
[TD]SubReq[/TD]
[TD]60P5700000A001[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]6[/TD]
[TD][/TD]
[TD="align: right"]6213[/TD]
[/TR]
[TR]
[TD]1/17/2016[/TD]
[TD][/TD]
[TD]SubReq[/TD]
[TD]60P5700000A001[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]5[/TD]
[TD][/TD]
[TD="align: right"]6217[/TD]
[/TR]
[TR]
[TD]1/21/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]4[/TD]
[TD]1008[/TD]
[TD]GVI/6200[/TD]
[/TR]
[TR]
[TD]1/27/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]3[/TD]
[TD]1008[/TD]
[TD]GVI/6202[/TD]
[/TR]
[TR]
[TD]2/3/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]2[/TD]
[TD]1008[/TD]
[TD]GVI/6203[/TD]
[/TR]
[TR]
[TD]2/5/2016[/TD]
[TD][/TD]
[TD]SubReq[/TD]
[TD]60P5700000A001[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]1[/TD]
[TD][/TD]
[TD="align: right"]6220[/TD]
[/TR]
[TR]
[TD]2/9/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]0[/TD]
[TD]1008[/TD]
[TD]GVI/6204[/TD]
[/TR]
[TR]
[TD]2/16/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]-1[/TD]
[TD]1008[/TD]
[TD]GVI/6205[/TD]
[/TR]
</tbody>[/TABLE]
Example of Sheet2
[TABLE="width: 825"]
<tbody>[TR]
[TD]Planned dates[/TD]
[TD]Needed Date after Changes[/TD]
[TD]MRP elemnt[/TD]
[TD]MRP elmnt data[/TD]
[TD]Rescheduling date[/TD]
[TD]Exception[/TD]
[TD]Rec./reqd qty[/TD]
[TD]Avail. quantity[/TD]
[TD]Stor. Loc.[/TD]
[TD]Model/Eff[/TD]
[/TR]
[TR]
[TD]1/28/2016[/TD]
[TD][/TD]
[TD]POitem[/TD]
[TD]4500018402/00010[/TD]
[TD][/TD]
[TD][/TD]
[TD]6[/TD]
[TD]6[/TD]
[TD]3003[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3/1/2016[/TD]
[TD][/TD]
[TD]POitem[/TD]
[TD]4500018402/00010[/TD]
[TD][/TD]
[TD][/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]3003[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3/22/2016[/TD]
[TD][/TD]
[TD]POitem[/TD]
[TD]4500018402/00010[/TD]
[TD][/TD]
[TD][/TD]
[TD]6[/TD]
[TD]6[/TD]
[TD]3003[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4/12/2016[/TD]
[TD][/TD]
[TD]POitem[/TD]
[TD]4500018402/00010[/TD]
[TD][/TD]
[TD][/TD]
[TD]5[/TD]
[TD]5[/TD]
[TD]3003[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
This is my first post, so please forgive me if I post any noob/dumb stuff with this post. I'm a bit stumped though with regard to writing this macro. The premise of the Macro is to simulate MRP within Excel. The first procedure is a simple calculation that takes the starting stock level and adds it to the requirement to its left for each row until there are no more requirements. That part works dandy, but will take any all suggestions to improve.
The 2nd sub is the part that is throwing me for a loop. Pun intended. So once I have my "Line of Balance" calculated, I want to find the first negative in Column H and insert a row before that negative value and then copy and paste a different row from worksheet2 that represent the PO. Once that line is now added to the "Line of Balance", I want my loop to do the calculation again find the next negative value and so on....The catch is, I only want to insert a row in sheet 1 if there are rows available in sheet 2 that contain a PO line. Once there are no more PO lines, I want a msgbox to appear and say no more POs or something and the end the loop. The code below doesnt seem to pick up both of these conditions and instead processes every negative before recalculating and wants to insert a row regardless if it is a PO row.
Again excuse the poor excuse of example below, couldn't figure out the screenshot insert. Noob....
Thank you!
Example of Sheet1
[TABLE="width: 782"]
<tbody>[TR]
[TD]Planned dates[/TD]
[TD]Needed Date after Changes[/TD]
[TD]MRP elemnt[/TD]
[TD]MRP elmnt data[/TD]
[TD]Rescheduling date[/TD]
[TD]Exception[/TD]
[TD]Rec./reqd qty[/TD]
[TD]Avail. quantity[/TD]
[TD]Stor. Loc.[/TD]
[TD]Model/Eff[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Stock[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]0.000[/TD]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1/1/2016[/TD]
[TD][/TD]
[TD]SubReq[/TD]
[TD]60P5700000A001[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]6[/TD]
[TD][/TD]
[TD="align: right"]6213[/TD]
[/TR]
[TR]
[TD]1/17/2016[/TD]
[TD][/TD]
[TD]SubReq[/TD]
[TD]60P5700000A001[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]5[/TD]
[TD][/TD]
[TD="align: right"]6217[/TD]
[/TR]
[TR]
[TD]1/21/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]4[/TD]
[TD]1008[/TD]
[TD]GVI/6200[/TD]
[/TR]
[TR]
[TD]1/27/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]3[/TD]
[TD]1008[/TD]
[TD]GVI/6202[/TD]
[/TR]
[TR]
[TD]2/3/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]2[/TD]
[TD]1008[/TD]
[TD]GVI/6203[/TD]
[/TR]
[TR]
[TD]2/5/2016[/TD]
[TD][/TD]
[TD]SubReq[/TD]
[TD]60P5700000A001[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]1[/TD]
[TD][/TD]
[TD="align: right"]6220[/TD]
[/TR]
[TR]
[TD]2/9/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]0[/TD]
[TD]1008[/TD]
[TD]GVI/6204[/TD]
[/TR]
[TR]
[TD]2/16/2016[/TD]
[TD][/TD]
[TD]DepReq[/TD]
[TD]60P2820021N003P02[/TD]
[TD][/TD]
[TD][/TD]
[TD]-1[/TD]
[TD]-1[/TD]
[TD]1008[/TD]
[TD]GVI/6205[/TD]
[/TR]
</tbody>[/TABLE]
Example of Sheet2
[TABLE="width: 825"]
<tbody>[TR]
[TD]Planned dates[/TD]
[TD]Needed Date after Changes[/TD]
[TD]MRP elemnt[/TD]
[TD]MRP elmnt data[/TD]
[TD]Rescheduling date[/TD]
[TD]Exception[/TD]
[TD]Rec./reqd qty[/TD]
[TD]Avail. quantity[/TD]
[TD]Stor. Loc.[/TD]
[TD]Model/Eff[/TD]
[/TR]
[TR]
[TD]1/28/2016[/TD]
[TD][/TD]
[TD]POitem[/TD]
[TD]4500018402/00010[/TD]
[TD][/TD]
[TD][/TD]
[TD]6[/TD]
[TD]6[/TD]
[TD]3003[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3/1/2016[/TD]
[TD][/TD]
[TD]POitem[/TD]
[TD]4500018402/00010[/TD]
[TD][/TD]
[TD][/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]3003[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3/22/2016[/TD]
[TD][/TD]
[TD]POitem[/TD]
[TD]4500018402/00010[/TD]
[TD][/TD]
[TD][/TD]
[TD]6[/TD]
[TD]6[/TD]
[TD]3003[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4/12/2016[/TD]
[TD][/TD]
[TD]POitem[/TD]
[TD]4500018402/00010[/TD]
[TD][/TD]
[TD][/TD]
[TD]5[/TD]
[TD]5[/TD]
[TD]3003[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Code:
Option Explicit
Dim Stock As Variant
Dim QPS As Variant
Dim QPSMultiple As Variant
Dim LOB As Range
Dim Cell As Range
Dim MRPElement As Range
Dim C2 As String
Sub LOB_Cal()
'Calculates LOB through all listed requirements
Worksheets(1).Range("H3").Select
Do Until ActiveCell.Offset(0, -1) = Empty
ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + ActiveCell.Offset(0, -1).Value
ActiveCell.Offset(1, 0).Select
Loop
Worksheets(1).Range("H2").Select
End Sub
Sub Find_First_Negative_Value_In_LOB()
Set LOB = Worksheets(1).Range("H3:H150").Cells
Set MRPElement = Worksheets(2).Range("C2:C150")
If InStr(1, Worksheets(2).Range("C2").Value, "POitem", vbTextCompare) <> 0 Then
For Each Cell In LOB
If Cell.Value < 0 Then
ActiveCell.EntireRow.Insert
Worksheets(2).Activate
ActiveSheet.UsedRange.AutoFilter Field:=3, Criteria1:="POitem"
ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Rows(1).EntireRow.Copy
Worksheets(1).Activate
Range("A1").End(xlDown).Offset(1, 0).Select
ActiveCell.Select
ActiveSheet.Paste
Worksheets(2).Activate
ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Rows(1).Delete
Worksheets(1).Activate
Worksheets(1).Range("F2").Select
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.Offset(0, -1) = Empty
ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + ActiveCell.Offset(0, -1).Value
Worksheets(1).Range("H3").Select
Loop
End If
Next Cell
For Each Cell In LOB
If Cell.Value >= 0 Then
Do Until ActiveCell.Offset(0, -1) = Empty
ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + ActiveCell.Offset(0, -1).Value
Worksheets(1).Range("H3").Select
Loop
End If
Next Cell
End If
If InStr(1, Worksheets(2).Range("C2").Value, "POitem", vbTextCompare) = 0 Then
MsgBox ("No More POs to Select")
Worksheets(1).Range("H2").Select
End If
End Sub