Option Compare Text 'la casse est ignorée (sécurité)
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Not Sh.Name Like "Conso critère*" Then Exit Sub
Dim Pref As Range, Pdest As Range, tref, tdest, ncol%, d As Object, i&
Dim critere$, tablo, w As Worksheet, t1, t2, j&, lig&, k%
Application.ScreenUpdating = False
Application.EnableEvents = False 'désactive les évènements
On Error Resume Next
'---préparation et RAZ---
Set Pref = Sh.[A14:A103] 'à adapter
Set Pdest = Sh.[D14:F103] 'à adapter
Pdest.SpecialCells(xlCellTypeConstants) = "" 'RAZ
tref = Pref 'matrice, plus rapide
tdest = Pdest.Formula 'matrice, plus rapide
ncol = UBound(tdest, 2)
'---liste des références et repérage de leurs lignes---
Set d = CreateObject("Scripting.dictionary")
d.CompareMode = vbTextCompare 'la casse est ignorée
For i = 1 To UBound(tref)
If tref(i, 1) <> "" Then d(tref(i, 1)) = i
Next i
'---analyse des feuilles et remplissage du tableau tdest---
critere = Replace(Sh.Name, " et ", Chr(1))
critere = Chr(1) & Trim(Mid(critere, 15)) & Chr(1)
tablo = Sheets("Liste").[A1].CurrentRegion.Resize(, 3)
For i = 2 To UBound(tablo)
If InStr(Chr(1) & tablo(i, 2) & Chr(1) & tablo(i, 3) & Chr(1), critere) Then
Set w = Nothing
Set w = Sheets(CStr(tablo(i, 1)))
If Not w Is Nothing Then
t1 = w.Range(Pref.Address) 'matrice, plus rapide
t2 = w.Range(Pdest.Address) 'matrice, plus rapide
For j = 1 To UBound(t1)
If d.exists(t1(j, 1)) Then
lig = d(t1(j, 1)) 'récupération de la ligne
For k = 1 To ncol
If t2(j, k) <> "" Then tdest(lig, k) = Val(Replace(tdest(lig, k), ",", ".")) + Val(Replace(t2(j, k), ",", "."))
Next k
End If
Next j
End If
End If
Next i
'---restitution---
Pdest = tdest
Application.EnableEvents = True 'réactive les évènements
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Workbook_SheetActivate Sh
End Sub