Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim pl As Range 'déclare la variable pl (PLage)
Dim r As Range 'déclare la variable r (Recherche)
Dim Cancel As Boolean
Dim Q As Integer
Dim Rep As Integer
Dim n
Dim l
Dim Somme
Dim Somme1
Dim Somme2
If Target.Count > 1 Then Exit Sub
'si la modification a lieu ailleurs qu'en A21:A106, sort de la procédure
If Intersect(Target, Range("A21:A106")) Is Nothing Then Exit Sub
Target = UCase(Target)
With Sheets("Fournisseurs") 'prend en compte l'onglet "Fournisseurs"
Set pl = .Range("A2:A" & .Range("A65536").End(xlUp).Row) 'définit la plage de recherche
End With 'fin de la prise en compte de l'onglet "Fournisseurs"
Set r = pl.Find(Target.Value, , xlValues, xlWhole) 'définit la recherche
If Not r Is Nothing Then 'condition : si il existe au moins une occurrence de B dans la plage pl
'place le résultat en B,C,E
Target.Offset(0, 1).Value = r.Offset(0, 1).Value
Target.Offset(0, 2).Value = r.Offset(0, 2).Value
Target.Offset(0, 4).Value = r.Offset(0, 4).Value
Else 'sinon
MsgBox "Code non trouvé !" 'message
Exit Sub
End If
'si A est effacée, B,C,D,E,F sont effacées également, sort de la procédure
If Target.Value = "" Then
Target.Offset(0, 2).Value = ""
Target.Offset(0, 3).Value = ""
Target.Offset(0, 4).Value = ""
Target.Offset(0, 5).Value = ""
Target.Offset(0, 6).Value = ""
'on recalcule les montants en fonction des suppressions
With Sheets("Devis").Range("D:D")
Somme = 0
Somme1 = 0
Somme2 = 0
Set n = .Find(what:="T.V.A", LookIn:=xlValues, lookat:=xlWhole)
If Not n Is Nothing Then
Somme = Application.WorksheetFunction.Sum(Range("F21:F" & (n.Row - 2))) + Somme
Somme1 = Somme * n(1, 2) + Somme1
Somme2 = Somme + Somme1 + Somme2
End If
n(0, 3) = Somme 'Montant H.T
n(1, 3) = Somme1 'Montant T.V.A
n(2, 3) = Somme2 'Montant T.T.C
End With
: Exit Sub
End If
'ouvre une boite de dialogue pour saisir la quantité
If Cells(Target.Row, 4).Value = "" Then
Q = InputBox("Saisir Quantité")
Cells(Target.Row, 4) = Q
Cells(Target.Row, 6).Value = Cells(Target.Row, 4).Value * Cells(Target.Row, 5).Value
End If
'dès que l'on trouve T.V.A, calcul des différents montants
With Sheets("Devis").Range("D:D")
Somme = 0
Somme1 = 0
Somme2 = 0
Set n = .Find(what:="T.V.A", LookIn:=xlValues, lookat:=xlWhole)
If Not n Is Nothing Then
Somme = Application.WorksheetFunction.Sum(Range("F21:F" & (n.Row - 2))) + Somme
Somme1 = Somme * n(1, 2) + Somme1
Somme2 = Somme + Somme1 + Somme2
End If
n(0, 3) = Somme 'Montant H.T
n(1, 3) = Somme1 'Montant T.V.A
n(2, 3) = Somme2 'Montant T.T.C
If Target.Address = "$A$49" Then
If MsgBox("Voulez-vous insérer 55 lignes ?", vbQuestion + vbYesNo) = vbNo Then Exit Sub
Application.EnableEvents = False
Rows(Target.Row & ":" & Target.Row + 55).Insert Shift:=xlShiftDown
Application.EnableEvents = True
End If
End With
End Sub