Option Explicit
Sub ProdPrévuEtUnAjustement()
Dim wkb As Workbook
Set wkb = Workbooks(ThisWorkbook.Name)
Dim wksBase As Worksheet
Dim wksNomenclatures As Worksheet
Set wksBase = wkb.Worksheets("Base")
Set wksNomenclatures = wkb.Worksheets("Nomenclatures")
Dim TBase, TNomenclatures As Variant
TBase = wksBase.Range(wksBase.Cells(2, 1), wksBase.Cells(wksBase.Cells(65536, 1).End(xlUp).Row, 3)).Value2
ReDim Preserve TBase(LBound(TBase, 1) To UBound(TBase, 1), LBound(TBase, 2) To UBound(TBase, 2) + 1)
TNomenclatures = wksNomenclatures.Range(wksNomenclatures.Cells(2, 1), wksNomenclatures.Cells(wksNomenclatures.Cells(65536, 1).End(xlUp).Row, 3)).Value2
ReDim Preserve TNomenclatures(LBound(TNomenclatures, 1) To UBound(TNomenclatures, 1), LBound(TNomenclatures, 2) To UBound(TNomenclatures, 2) + 1)
Dim Rgn As Range
Dim Coll As Collection
Dim i, j As Integer
Set Coll = New Collection
For i = LBound(TBase, 1) To UBound(TBase, 1)
On Error Resume Next
Coll.Add (TBase(i, 1) & Chr(27) & i), Key:=TBase(i, 1)
On Error GoTo 0
Next i
For i = 1 To Coll.Count
For j = LBound(TBase, 1) To UBound(TBase, 1)
If TBase(j, 1) = Split(Coll.Item(i), Chr(27))(0) Then
If TBase(j, 2) = "Ajustement prod" Or TBase(j, 2) = "Prod prévue" Then
Set Rgn = wksBase.Range(wksBase.Cells(j + 1, 3), (wksBase.Cells(j + 1, 3)))
If Rgn.Interior.ColorIndex = 4 Then
TBase(CInt(Split(Coll.Item(i), Chr(27))(1)), 4) = TBase(CInt(Split(Coll.Item(i), Chr(27))(1)), 4) + TBase(j, 3)
End If
End If
End If
Next j
Next i
' Remplire la Colonne D "Nomemclature"
For i = LBound(TBase, 1) To UBound(TBase, 1) Step 3
For j = LBound(TNomenclatures, 1) To UBound(TNomenclatures, 1)
If TBase(i, 1) = TNomenclatures(j, 1) Then
TNomenclatures(j, 4) = TBase(i, 4)
End If
Next j
Next i
wksNomenclatures.Cells(2, 4).Resize(UBound(TNomenclatures, 1), 1).Value = Application.Index(TNomenclatures, , 4)
End Sub