1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| Private Sub Ajouter_Click() '--- Bouton Ajouter
Dim onglet As Worksheet
Dim derniere_ligne As Long
Dim i As Long
Set onglet = Worksheets("RJ")
derniere_ligne = onglet.Cells(Rows.Count, 1).End(xlUp).Row
If ComboBox7.Value = "" Then '--- ComboBox7 = référence
'--- ajoute une ligne et une référence
i = derniere_ligne + 1
If i = 2 Then
onglet.Cells(i, 1) = 1
Else
onglet.Cells(i, 1) = onglet.Cells(i - 1, 1) + 1
End If
Else
'--- se place sur la ligne ayant la référence
For i = 2 To derniere_ligne
If onglet.Cells(i, 8) = ComboBox7.Value Then Exit For
Next i
End If
With onglet
If i > 2 Then
'--- recopie le formatage effectué en ligne n°2
.Range("A2:T2").Copy
Cells(i, 1).PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
End If
.Cells(i, 2) = ComboBox1.Value
.Cells(i, 3) = ComboBox2.Value
.Cells(i, 4) = DateValue(Format(Date, "dd/mm/yyyy"))
If TextBox11.Value <> "" Then
.Cells(i, 5) = CDate(TextBox11.Value)
Else
.Cells(i, 5) = ""
End If
.Cells(i, 6) = Nom.Value
.Cells(i, 7) = Prenom.Value
'formule concatenate pour recherche
.Cells(i, 8).FormulaR1C1 = "=IF(RC[-2]="""","""",IF(RC[1]="""",CONCATENATE(RC[-2],"" / "",RC[-1]),CONCATENATE(RC[-2],"" / "",RC[-1],"" - "",TEXT(RC[1],""jj/mm/aaaa""))))"
If TextBox2.Value <> "" Then
.Cells(i, 9) = CDate(TextBox2.Value)
Else
.Cells(i, 9) = ""
End If
.Cells(i, 10) = TextBox3.Value
.Cells(i, 11) = TextBox4.Value
.Cells(i, 12) = ComboBox3.Value
.Cells(i, 13) = TextBox5.Value
.Cells(i, 14) = ComboBox4.Value
If TextBox7.Value <> "" Then
.Cells(i, 15) = CDate(TextBox7.Value)
Else
.Cells(i, 15) = ""
End If
'formule calcul date surveillance
.Cells(i, 16).FormulaR1C1 = "=IF(RC[-1]="""","""",TODAY()-RC[-1])"
If TextBox12.Value <> "" Then
.Cells(i, 17) = CDate(TextBox12.Value)
Else
.Cells(i, 17) = ""
End If
.Cells(i, 18) = ComboBox5.Value
.Cells(i, 19) = ComboBox6.Value
.Cells(i, 20) = TextBox6.Value
End With
Unload UserForm1
UserForm1.Show
End Sub |