Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column > 1 Then Exit Sub
' Saisie d'un nouveau client'
Dim MemClt As String, CltPrec As String, VCol As Integer, VLig As Integer
' On mémorise le nom du client'
MemClt = Target.Value
If MemClt = "" Then
' Si le nom du client est vide = effacer'
' On récupère le nom du client précédent'
CltPrec = Target.Offset(-1, 0)
End If
' 1- tri de la colonne'
Range("A:A").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
' Mise à jour de la colonne'
Application.EnableEvents = False
If MemClt <> "" Then
' 2- trouver le client qui le précède dans la ligne'
VLig = LigFind("Datas", 1, MemClt) - 1
CltPrec = Cells(VLig, 1)
' 3- Trouver ce client dans les colonnes'
VCol = ColFind("Datas", 1, CltPrec) + 1
Columns(VCol).Insert Shift:=xlToRight
Cells(1, VCol).Value = "Projets " & MemClt
Cells(2, VCol).Value = "Site"
Else
' 3- Trouver ce client dans les colonnes'
VCol = ColFind("Datas", 1, CltPrec) + 1
Columns(VCol).Delete Shift:=xlToLeft
End If
Application.EnableEvents = True
End Sub
Function ColFind(Feuil As String, NumLig As Integer, Quoi)
On Error Resume Next
With Sheets(Feuil).Rows(NumLig)
ColFind = .Find(What:=Quoi, LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext, _
SearchOrder:=xlByColumns, MatchCase:=False).Column
End With
On Error GoTo 0
End Function
Function LigFind(Feuil As String, NumCol As Integer, Quoi)
On Error Resume Next
With Sheets(Feuil).Columns(NumCol)
If Left(Quoi, 1) = "=" Then
' On recherche une formule'
LigFind = .Find(What:=Quoi, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False).Row
Else
' On recherche une valeur'
LigFind = .Find(What:=Quoi, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False).Row
End If
End With
On Error GoTo 0
End Function