Sub Place_demand()
Dim i As Integer, j As Integer, K As Long
Dim cell As Range
Dim ws As Worksheet
Dim Product_Table As Range
Dim NbProduct As Integer
Dim MonthPosition As Integer
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
Set sh1 = ThisWorkbook.Worksheets("Données")
Set sh2 = ThisWorkbook.Worksheets("Demande")
Set sh3 = ThisWorkbook.Worksheets("Prévision")
'Compte le nombre de produits dans la feuille Demande
With sh2.Range("B1")
Set Product_Table = Range(.Offset(0, 0), .End(xlToRight))
End With
NbProduct = Product_Table.Columns.Count 'Compte le nombre de produits
'Renvoit le numéro de ligne dans lequel se trouve le mois choisit dans la feuille Données
Dim Numligne As Integer
For i = 1 To sh2.Range("A" & Rows.Count).End(xlUp).Row
If sh1.Cells(1, 7) = sh2.Cells(i, 1) Then
'la date a été trouvée
Numligne = sh2.Cells(i, 1).Row
Exit For
End If
Next i
If i > sh2.Range("A" & Rows.Count).End(xlUp).Row Then
'La date n'a pas été trouvée -> on la rajoute
MsgBox "Création du nouveau mois: " & sh1.Cells(1, 7).Text
sh2.Range("A" & Rows.Count).End(xlUp).Offset(1) = sh1.Cells(1, 7)
Numligne = sh2.Range("A" & Rows.Count).End(xlUp).Row
End If
'Met la valeur des commandes du mois approprié pour chaque produit
For i = 2 To sh1.Range("A" & Rows.Count).End(xlUp).Row
K = sh2.Cells(1, Columns.Count).End(xlToLeft).Column
For j = 2 To K
'rérérence trouvée
If sh1.Cells(i, 2) = sh2.Cells(1, j) Then
sh2.Cells(Numligne, j) = sh1.Cells(i, 4)
Exit For
End If
Next j
If j > K Then
'La référence n'a pas été trouvée on la rajoute
MsgBox "Nouveau REF produit " & sh1.Cells(i, 2) & " -> " & sh1.Cells(i, 3)
sh2.Cells(1, K + 1) = sh1.Cells(i, 2)
sh2.Cells(2, K + 1) = sh1.Cells(i, 3)
sh2.Cells(Numligne, K + 1) = sh1.Cells(i, 4)
End If
Next i
End Sub