Option Explicit
Sub test()
Dim a, w(), i As Long, j As Long, txt As String, txt1 As String
Dim dico As Object
Set dico = CreateObject("scripting.dictionary")
dico.CompareMode = 1
With Sheets("RESULTAT")
txt1 = Join$(Array(.Range("a2").Value, .Range("a4").Value, .Range("b3").Value), Chr(2))
End With
dico(txt1) = Empty
With Sheets("BD").Range("a1").CurrentRegion
a = .Value
For i = 2 To UBound(a, 1)
txt = Join$(Array(a(i, 2), a(i, 3), a(i, 7)), Chr(2))
If dico.exists(txt) Then
If IsEmpty(dico(txt)) Then
ReDim w(1 To 2)
ReDim x(1 To 14, 1 To 2)
x(1, 1) = Split(txt, Chr(2))(2) & " - " & Split(txt, Chr(2))(0) & " - " & Split(txt, Chr(2))(1)
For j = 1 To 12
x(j + 1, 1) = MonthName(j)
Next
Set w(2) = CreateObject("Scripting.Dictionary")
w(2).CompareMode = 1
Else
w = dico(txt)
x = w(1)
End If
If Not w(2).exists(a(i, 5)) Then
w(2)(a(i, 5)) = Empty
ReDim Preserve x(1 To 14, 1 To UBound(x, 2) + 1)
x(1, UBound(x, 2) - 1) = a(i, 5)
End If
x(Application.Match(MonthName(a(i, 6)), Application.Index(x, , 1), 0), Application.Match(a(i, 5), Application.Index(x, 1), 0)) = "x"
w(1) = x
dico(txt) = w
End If
Next
If Not IsEmpty(dico(txt1)) Then
w = dico(txt1): x = w(1)
x(1, UBound(x, 2)) = "Nombre d'espèces observées"
x(UBound(x, 1), 1) = "Nombre de l'espèce observée"
For i = 2 To UBound(x, 1) - 1
x(i, UBound(x, 2)) = Application.CountA(Application.Index(x, i, Evaluate("row(2:" & UBound(x, 2) - 1 & ")")))
Next
For i = 2 To UBound(x, 2) - 1
x(UBound(x, 1), i) = Application.CountA(Application.Index(x, Evaluate("row(2:" & UBound(x, 1) - 1 & ")"), i))
Next
x(UBound(x, 1), UBound(x, 2)) = Application.Sum(Application.Index(x, UBound(x, 1), Evaluate("row(2:" & UBound(x, 2) - 1 & ")")))
w(1) = x: dico(txt1) = w
End If
End With
'Restitution en Feuil1
Application.ScreenUpdating = False
With Sheets("Feuil1").Range("a1")
.CurrentRegion.Clear
If IsEmpty(dico(txt1)) Then
MsgBox "Pas d'espèces observées"
Else
w = dico(txt1)(1)
With .Resize(UBound(w, 2), UBound(w, 1))
.Value = Application.Transpose(w)
.Font.Size = 10
.Rows(1).BorderAround Weight:=xlThin
.BorderAround Weight:=xlThin
.Borders(xlInsideVertical).Weight = xlThin
.VerticalAlignment = xlCenter
With .Offset(1).Resize(.Rows.Count - 2, 1)
.Interior.ColorIndex = 36
End With
With .Offset(, 1).Resize(1, .Columns.Count - 1)
.Interior.ColorIndex = 44
End With
With .Offset(, 1).Resize(, .Columns.Count - 1)
.HorizontalAlignment = xlCenter
.Columns.ColumnWidth = 8
End With
With .Rows(.Rows.Count)
.BorderAround Weight:=xlThin
With .Resize(, .Columns.Count - 1)
.Interior.ColorIndex = 35
End With
End With
.Columns(1).AutoFit
.Columns(.Columns.Count).AutoFit
End With
End If
.Parent.Activate
End With
Set dico = Nothing
Application.ScreenUpdating = True
End Sub