Private Sub Workbook_Open()
Dim ncol%, d As Object, F As Worksheet, n&, fichier$, w As Worksheet, tablo, i&, x$, s, j%, y$, z$, resu(), k&, col%, ub&
ncol = 6 'nombre de colonnes des résultats
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare 'la casse est ignoré
Application.ScreenUpdating = False
For Each F In Me.Worksheets 'adapter si nécessaire aux feuilles à traiter
d.RemoveAll 'RAZ
n = 0
fichier = Me.Path & "\" & F.Name & ".xlsx" 'même nom que la feuille
If Dir(fichier) = "" Then MsgBox "Fichier '" & fichier & "' introuvable !", 48: GoTo 2
Workbooks.Open fichier 'ouvre le fichier
'---feuilles Methodes et Actes---
For Each w In ActiveWorkbook.Sheets(Array("Methodes", "Actes"))
tablo = w.[A2].CurrentRegion.Resize(, 5) 'matrice, plus rapide
For i = 2 To UBound(tablo)
x = tablo(i, 1)
s = Split(tablo(i, 3), ";")
For j = 0 To UBound(s)
y = Trim(s(j))
If y <> "" Then
z = x & y
If Not d.exists(z) Then
n = n + 1
d(z) = n 'mémorise la ligne
ReDim Preserve resu(1 To ncol, 1 To n)
resu(1, n) = x 'Numéro
resu(4, n) = y 'Intervenant
End If
k = d(z)
If w.Name = "Methodes" Then
y = Chr(1) & Application.Proper(tablo(i, 4)) & Chr(1) 'encadrement
If InStr(resu(5, k), y) = 0 Then resu(5, k) = resu(5, k) & y 'concaténation des Méthodes sans doublon
Else
y = Chr(1) & UCase(tablo(i, 5)) & Chr(1) 'encadrement
If InStr(resu(6, k), y) = 0 Then resu(6, k) = resu(6, k) & y 'concaténation des UO sans doublon
End If
End If
Next j, i, w
'---feuille Bons---
d.RemoveAll 'RAZ
tablo = ActiveWorkbook.Sheets("Bons").[A2].CurrentRegion.Resize(, 3) 'matrice, plus rapide
For i = 2 To UBound(tablo)
x = tablo(i, 1)
If Not d.exists(x) Then d(x) = i 'mémorise la ligne
Next i
For i = 1 To n
x = resu(1, i)
If d.exists(x) Then
k = d(x)
If IsDate(tablo(k, 2)) Then resu(2, i) = CDate(tablo(k, 2)) Else resu(2, i) = tablo(k, 2)
resu(3, i) = tablo(k, 3)
End If
Next i
If n = 0 Then GoTo 1 'si le tableau est vide
'---déconcaténations et redéfinitions du tableau resu---
For col = 5 To 6
tablo = resu: Erase resu: n = 0 'RAZ
For i = 1 To UBound(tablo, 2)
s = Split(tablo(col, i), Chr(1))
ub = UBound(s)
If ub = -1 Then
n = n + 1
ReDim Preserve resu(1 To ncol, 1 To n)
For j = 1 To ncol
resu(j, n) = tablo(j, i)
Next j
Else
'For k = 0 To ub
For k = 1 To ub - 1
If s(k) <> "" Then
n = n + 1
ReDim Preserve resu(1 To ncol, 1 To n)
tablo(col, i) = s(k)
For j = 1 To ncol
resu(j, n) = tablo(j, i)
Next j
End If
Next k
End If
Next i, col
'---transposition---
ReDim tablo(1 To n, 1 To ncol)
For i = 1 To n
For j = 1 To ncol
tablo(i, j) = resu(j, i)
Next j, i
'---restitution---
1 ActiveWorkbook.Close False 'ferme le fichier
2 With F
If .FilterMode Then .ShowAllData 'si la feuille est filtrée
With .[A3] '1ère cellule de destination, à adapter
If n Then
.Resize(n, ncol) = tablo
.Resize(n, ncol).Borders.Weight = xlThin 'bordures
End If
.Offset(n).Resize(Rows.Count - n - .Row + 1, ncol).Delete xlUp 'RAZ en dessous
End With
With .UsedRange: End With 'actualise la barre de défilement verticale
End With
Next F
End Sub