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
| Sub Test_VB()
Dim DataTabAR() As Variant
Dim InpRng As Range
Dim ColN As Integer, RowN As Integer, DataTabRowN As Integer, RowOffs As Integer
Dim CatInd As Integer, CatMax As Integer
Dim LabSplit As Variant
' Initialisation
Set InpRng = ThisWorkbook.Worksheets("Feuil1").Range("A1").CurrentRegion 'On définit le range de départ
Debug.Print InpRng.AddressLocal
ReDim DataTabAR(1 To InpRng.Rows.Count, 1 To InpRng.Rows.Count)
' On S'assure que les données d'entrée sont bien triées: ça peut être enlevé si c'est nativement le cas
With InpRng.Worksheet.AutoFilter.Sort
.SortFields.Clear
.SortFields.Add Key:=InpRng.Columns(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=InpRng.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' On lit le nom des agents
ColN = 1
DataTabRowN = 1
RowOffs = InpRng.Rows.Count + 2
DataTabColN = 1
For RowN = 2 To InpRng.Rows.Count
If InpRng(RowN, ColN + 1).Value Like "N# *" = True Then 'Cellule commencant par 'N(0-9) '
If InpRng(RowN, ColN).Value <> InpRng(RowN - 1, ColN).Value Then DataTabRowN = DataTabRowN + 1 'si c'est le même nom, on reste sur la même ligne autrement saut
LabSplit = Split(InpRng(RowN, ColN + 1), " ", , vbTextCompare)(0) 'On Split sur l'espace
CatInd = CInt(Right(LabSplit, Len(LabSplit) - 1)) 'On récupère la valeur après le N pour recalculer la colonne après
If CatInd > CatMax Then CatMax = CatInd
' On écrit dessous
InpRng(DataTabRowN + RowOffs, ColN) = InpRng(RowN, ColN)
InpRng(DataTabRowN, ColN + CatInd) = InpRng(RowN, ColN + 1)
End If
Next RowN
' On écrit les titres
InpRng(RowOffs, ColN) = InpRng(1, ColN)
For ColN = 1 To CatMax
InpRng(RowOffs, ColN + 1) = "N" & ColN
Next ColN
' On peut effacer le range source
'InpRng.Clear
End Sub |
Partager