Sub trueinv()
'
'Takes stock of raw products processed into others
'
'Worksheet Variables
Dim insert As Worksheet
Dim bom As Worksheet
Dim output As Worksheet
Dim purch As Worksheet
Dim inter As Worksheet
'Rowcount Variables
Dim BomCount As Integer
Dim InsCount As Integer
Dim InterCount As Integer
Dim PurchCount As Integer
'Loop Variable
Dim i As Integer
'Find Variables
Dim FoundRange As Range
Dim ItemId As String
'Add Stock variable
Dim AddStock As Double
Dim CurrentStock As Double
'Set sheet variables
Set insert = Sheets("insert")
Set bom = Sheets("boms")
Set output = Sheets("outputs")
Set purch = Sheets("purchased")
Set inter = Sheets("intermediate")
'Capture eligible products into their own sheet
InsCount = insert.UsedRange.Rows.Count
insert.Range("a1:f" & InsCount).AutoFilter field:=4, Criteria1:="=*eligible*", Operator:=xlAnd
insert.Range("a1:f" & InsCount).Copy
purch.Range("a1").PasteSpecial
PurchCount = purch.UsedRange.Rows.Count
insert.Range("a1:f" & InsCount).AutoFilter field:=4
'First Loop of Bom Check: Determine if items are actually BOM'd
For i = 2 To PurchCount
ItemId = purch.Cells(i, 1).Value
Set FoundRange = bom.Range("b1:b" & BomCount).Find(what:=ItemId, LookIn:=xlFormulas, lookat:=xlWhole)
'Not a Bom
If FoundRange Is Nothing Then
purch.Rows(i).Copy
output.Range("a12000").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
Else
'Check First level Boms
purch.Rows(i).Copy
output.Range("a12000").End(xlUp).Offset(1, 0).PasteSpecial
bom.Range("a1:c" & BomCount).AutoFilter field:=2, Criteria1:="=" & ItemId, Operator:=xlAnd
BomCount = bom.UsedRange.Rows.Count
bom.Range("a2:c" & BomCount).Copy
'Add stock of first level boms
inter.Range("a2").PasteSpecial
InterCount = inter.UsedRange.Rows.Count
inter.Range("d2:d" & InterCount).FormulaR1C1 = "=vlookup(rc[-3],insert!r2c1:r12000c6,6,false)"
inter.Range("e2:e" & InterCount).FormulaR1C1 = "=rc[-2]*rc[-1]"
AddStock = Application.WorksheetFunction.Sum(inter.Range("e2:e" & InterCount))
CurrentStock = output.Range("a12000").End(xlUp).Offset(0, 5).Value
output.Range("a12000").End(xlUp).Offset(0, 5).Value = CurrentStock + AddStock
End If
'Determine if there are second level BOMs
Dim arr As Variant
InterCount = inter.UsedRange.Rows.Count
arr = inter.Range("a2:a" & InterCount).Value
BomCount = bom.UsedRange.Rows.Count
bom.Range("a1:c" & BomCount).AutoFilter field:=2, Criteria1:=WorksheetFunction.Transpose(arr), _
Operator:=xlFilterValues
End Sub