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
| Dim f1 As Worksheet, f2 As Worksheet
Dim l1 As Long, l2 As Long
Dim d1 As Object, d2 As Object
Private Sub CommandButton1_Click()
'--- Ajouter ton code qui enregistre les valeurs de modif dans ta Feuil1
' Tes modifications ici
'--- On cherche si la valeur de la Combobox existe dans la Feuil2
If IsNumeric(Me.ComboBox1.Value) = True Then
v = CLng(Me.ComboBox1.Value)
Else: v = Me.ComboBox1.Value
End If
'- Si l'ID existe en Feuil2, on copie colle la ligne au même emplacement
If d2.Exists(v) Then
f1.Rows(d1(v)).Copy f2.Rows(d2(v))
'- Sinon on l'insère en ligne 4 de la Feuil2
Else
f1.Rows(d1(v)).Copy: f2.Rows("4").Insert Shift:=xlDown
End If
'--- On décharge le presse papier
Application.CutCopyMode = False
'--- On décharge l'usf
Unload Me
End Sub
Private Sub UserForm_Initialize()
'--- On définit les variables
Set f1 = Feuil1: Set f2 = Feuil2
Set d1 = CreateObject("Scripting.Dictionary"): Set d2 = CreateObject("Scripting.Dictionary")
l1 = f1.Cells.Find("*", , , , xlByRows, xlPrevious).Row: l2 = f2.Cells.Find("*", , , , xlByRows, xlPrevious).Row
'--- On remplit les dictionary
'- Feuil1
For Each Cell In f1.Range("a1:a" & l1)
If Cell.Value <> "" Then d1(Cell.Value) = Cell.Row
Next Cell
'- Feuil2
For Each Cell In f2.Range("a1:a" & l2)
If Cell.Value <> "" Then d2(Cell.Value) = Cell.Row
Next Cell
'--- On alimente la Combobox
Me.ComboBox1.List = d1.Keys
End Sub |
Partager