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
| Option Explicit
Sub Test()
CommunsPlages_CountIf Range("A6:A25"), Range("B1:B17")
End Sub
'La procédure recherche et compte les données de Plage_A qui apparaissent
'aussi dans Plage_B
Sub CommunsPlages_CountIf(Plage_A As Range, Plage_B As Range)
Dim Cell As Range
Dim i As Integer, k As Integer
Dim Tableau() As Variant
'boucle sur les cellules de la plage Plage_A
For Each Cell In Plage_A
'Vérifie si la donnée de Plage_A apparait dans Plage_B
If Application.CountIf(Plage_B, Cell) > 0 Then
'Redimensionne le tableau de résultats
i = i + 1
ReDim Preserve Tableau(1 To 2, 1 To i)
'Compte le nombre de fois que la donnée apparait
'dans Plage_B.
k = Application.CountIf(Plage_B, Cell)
'--- Remplissage tableau:
'La donnée
Tableau(1, i) = Cell
'Le nombre de fois que la donnée apparait dans Plage_B.
Tableau(2, i) = k
'-----------------------
End If
Next Cell
'Copie le tableau de résultats dans la Feuil2
Worksheets("Feuil2").Range("A1:B" & i) = Application.Transpose(Tableau)
End Sub |
Partager