Sub Ecrire_Pièces()
Dim MesLignes, MaLigneEntrée, MaLigneSortie, MonSeuilAlerte As Integer
Dim MonLibellé, Mareference, MaCatégorie, MonDescriptif, MonStockage, MesObservations, Mesmagasin As String
Dim Formule As String, MonStockInitial As Single
On Error GoTo 1
With UF_Pièces
If .Titre.Caption = "Ajouter une Pièce" Then
' AJOUT *******************************************************
ModeLigne = "Ajout"
MaLigne = Range("Pièces").Row + 1
Else
'MODIF *******************************************************
ModeLigne = "Modif"
End If
MonLibellé = .TB_Libellé.Value
MaCatégorie = .CB_Catégorie.Value
MonStockInitial = .TB_Stock_Initial.Value
MonSeuilAlerte = .TB_Seuil_Alerte.Value
MonDescriptif = .TB_Descriptif.Value
Mareference = .TB_Reference.Value
MonStockage = .TB_Stockage.Value
MesObservations = .TB_Observations.Value
Mesmagasin = .TB_Magasin
End With
Sheets("Inventaire").Activate
Call Déprotéger
If ModeLigne = "Ajout" Then
Application.ScreenUpdating = False
With Sheets("Inventaire")
.Rows(MaLigne).Select
Selection.Copy
ActiveCell.EntireRow.Insert Shift:=xlDown
Application.CutCopyMode = False
End With
End If
With Sheets("Inventaire")
'Libellé (colonne A)
.Range("A" & MaLigne).Value = MonLibellé
'Catégorie (colonne B)
.Range("B" & MaLigne).Value = MaCatégorie
'stock initial (colonne C)
.Range("C" & MaLigne).Value = MonStockInitial
'Stock actuel (colonne D)
'=C4+J4-K4
Formule = "=C" & MaLigne & "+J" & MaLigne & "-K)" & MaLigne
.Range("D" & MaLigne).FormulaLocal = Formule
'Car 1 (colonne E)
.Range("E" & MaLigne).Value = MonSeuilAlerte
'Car 2 (colonne F)
.Range("F" & MaLigne).Value = MonDescriptif
'Car 2 (colonne G)
.Range("G" & MaLigne).Value = Mareference
'Car 3 (colonne H)
.Range("H" & MaLigne).Value = MonStockage
'Observations (colonne I)
.Range("I" & MaLigne).Value = MesObservations
'Entrées (colonne J)
'=SOMME.SI(Entrées!C:C;A:A;Entrées!E:E)
Formule = "=SOMME.SI.ENS(Entrées!E:E;Entrées!C:C;A:A;Entrées!I:I;L:L))"
.Range("J" & MaLigne).FormulaLocal = Formule
'Sorties (colonne K)
'=SOMME.SI(Sorties!B:B;A:A;Sorties!D:D)
Formule = "=SOMME.SI.ENS(Sorties!E:E;Sorties!C:C;A:A;Sorties!G:G;L:L))"
.Range("K" & MaLigne).FormulaLocal = Formule
'Observations (colonne L)
.Range("L" & MaLigne).Value = Mesmagasin
.Range("A" & MaLigne).Select
End With
1
Call Protéger
'Changement d'un libellé de Pièce impliquant une mise à jour des Entrées et Sorties et éventuellement de la valeur par défaut
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If ModeLigne <> "Modif" Or MonLibellé = MonAncienLibellé Then GoTo Fin
Maj_Entrées:
'************
MaLigneEntrée = Range("Entrées").Row + 1
Application.ScreenUpdating = False
With Sheets("Entrées")
.Unprotect
Boucle_MAJ_Entrées:
If Len(.Range("A" & MaLigneEntrée).Value) = 0 Then GoTo Maj_Sorties
If .Range("C" & MaLigneEntrée).Value = MonAncienLibellé Then .Range("C" & MaLigneEntrée).Value = MonLibellé
MaLigneEntrée = MaLigneEntrée + 1
GoTo Boucle_MAJ_Entrées
End With
Maj_Sorties:
'************
MaLigneSortie = Range("Sorties").Row + 1
Application.ScreenUpdating = False
With Sheets("Sorties")
.Unprotect
Boucle_MAJ_Sorties:
If Len(.Range("A" & MaLigneSortie).Value) = 0 Then GoTo Fin_Boucle_MAJ
If .Range("C" & MaLigneSortie).Value = MonAncienLibellé Then .Range("C" & MaLigneSortie).Value = MonLibellé
MaLigneSortie = MaLigneSortie + 1
GoTo Boucle_MAJ_Sorties
Fin_Boucle_MAJ:
.Protect DrawingObjects:=True, Contents:=True, Scenarios:= _
False, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
End With
If Range("VD_Pièce").Value = MonAncienLibellé Then Range("VD_Pièce").Value = MonLibellé
Fin:
Sheets("Inventaire").Activate
End Sub