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
| Private Sub Worksheet_Change(ByVal Target As Range)
Dim newPos As Integer, N As Integer, k As Integer
Dim Pos As Long, i As Long
Dim Tach() As String
Dim Plage As Range
Dim Tb
If Target.Count = 1 Then
N = 5 'Nombre de colonnes
ReDim Tach(2 To N)
Set Plage = Range("A45").Resize(10) 'Plage A45:A54 (10 lignes: 10 priorités)
Tb = Plage.Resize(, N).Value 'Dans une variable tableau on prend la matrice de 10 lignes et N colonnes
If Not Intersect(Target, Plage) Is Nothing Then 'Si un changement s'oppère sur la priorité en colonne A
If Target.Value <= Plage.Count And Val(Target.Value) > 0 Then 'si la priorité ne dépasse pas 10 (ou le nombre de cellules de A45:A54) et est un nombre >0
Pos = Target.Row - Plage.Row + 1 ' Pos la position de la ligne changée par rapport à la plage
newPos = Target.Value 'newPos la valeur entrée comme nouvelle priorité
For k = 2 To N
Tach(k) = Tb(Pos, k) 'On mémorise les données de la ligne qui vient d'être changée
Next k
If Pos < newPos Then 'Deux traitement distincts selon que pos>newpos ou nom
For i = Pos To newPos - 1
Tb(i, 1) = i
For k = 2 To N
Tb(i, k) = Tb(i + 1, k)
Next k
Next i
Else
For i = Pos To newPos + 1 Step -1
Tb(i, 1) = i
For k = 2 To N
Tb(i, k) = Tb(i - 1, k)
Next k
Next i
End If
For k = 2 To N
Tb(i, k) = Tach(k)
Next k
Application.EnableEvents = False
Plage.Resize(, N).Value = Tb 'On ré injecte le tableau sur notre plage
Application.EnableEvents = True
Else
Application.Undo
End If
End If
End If
End Sub |
Partager