Option Explicit
Dim d As Object
Sub RecapMachine()
Dim Fp As Worksheet, Fc As Worksheet, Tb(), dl As Integer, i As Integer, j As Integer
Dim plage As Range, C As Range
Set Fp = ThisWorkbook.Worksheets("personnel")
Set Fc = ThisWorkbook.Worksheets("feuil2") 'feuille à adapter
Set d = CreateObject("scripting.dictionary")
With Fp
dl = .Range("A" & Rows.Count).End(xlUp).Row
Tb = .Range("B2:AE" & dl).Value
End With
'on récupère tous les noms sans doublons en utilisant le dictionnaire 'd'
'pour dimensionner le Tableau Tt avec lequel on va récuperer le nombre de fois agent sur même machine
For i = LBound(Tb) To UBound(Tb)
For j = LBound(Tb, 2) To UBound(Tb, 2)
If Tb(i, j) <> "" Then d(Tb(i, j)) = ""
Next j
Next i
DicoTri d 'on tri pour avoir les noms triés
ReDim Tt(1 To d.Count - 1, 1 To 31) ' on dimensionne le tableau
'on alimente entete
For i = 2 To 31
Tt(1, i) = Fp.Cells(1, i)
Next i
' on alimente les noms
For i = 2 To d.Count - 1
Tt(i, 1) = d.keys()(i)
Next i
' on comptabilise les occurences des noms
For j = 2 To UBound(Tt, 2)
For i = 2 To UBound(Tt)
Set plage = Fp.Range(Fp.Cells(2, j), Fp.Cells(dl, j))
If Application.CountA(plage) = 0 Then
Exit For
Else
For Each C In plage
If C.Value = Tt(i, 1) Then Tt(i, j) = Tt(i, j) + 1
Next C
End If
Next i
Next j
'report sur la feuille
With Fc
.Cells.Clear
.Range("A1").Resize(UBound(Tt), UBound(Tt, 2)) = Tt
.Range("A1").CurrentRegion.Borders.Weight = xlThin
.Rows(1).Orientation = xlVertical
.Columns("A:AE").EntireColumn.AutoFit
With .Range("A1").CurrentRegion.Offset(1, 1)
.HorizontalAlignment = xlCenter
.Font.Bold = True
End With
End With
Set plage = Nothing
Set d = Nothing
Set Fc = Nothing: Set Fp = Nothing
End Sub
Sub DicoTri(dico) 'Source du code http://boisgontierj.free.fr/
Dim i As Integer, Tbl
Tbl = d.keys ' Transfert Dictionnaire dans Array
Tri Tbl, LBound(Tbl), UBound(Tbl) ' Tri Array
d.RemoveAll ' Création du dictionnaire
For i = LBound(Tbl) To UBound(Tbl)
d(Tbl(i)) = ""
Next i
End Sub
Sub Tri(a, gauc, droi) ' Quick sort 'Source du code http://boisgontierj.free.fr/
Dim ref As String, g As Integer, d As Integer, temp
ref = a((gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While a(g) < ref: g = g + 1: Loop
Do While ref < a(d): d = d - 1: Loop
If g <= d Then
temp = a(g): a(g) = a(d): a(d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call Tri(a, g, droi)
If gauc < d Then Call Tri(a, gauc, d)
End Sub