Compare range array

Hoareau

XLDnaute Occasionnel
Bonjour

La fonction ci-dessous compare un range et un array.

Si dans le range, il y a 2 fois la même valeur, c'est compté comme 2.

Je voudrais que les valeurs doubles dans le range ou l'array soient comptées, comme une seule valeur.

merci


Public Function Compare_Range_Array(Plage As Range, tblo As Variant)

Dim C As Range

Application.Volatile

For Each C In Plage
For Tblo_val = 0 To UBound(tblo)

If tblo(Tblo_val) = C.Value Then Compare_Range_Array = Compare_Range_Array + 1

Next
Next

End Function
 

mapomme

XLDnaute Barbatruc
Supporter XLD
Re : Compare range array

Bonjour Hoareau,

Un essai dans le fichier joint avec 4 exemples. les valeurs vides ne sont pas prises en compte. Cela devrait pouvoir fonctionner avec Tblo égal à un array, un range ou une constante.

VB:
Public Function Compare_Range_Array(Plage As Range, tblo As Variant)
Dim C, Tref(), Nref(), i As Long, N As Long

If VarType(tblo) > vbArray Then
  For Each C In tblo
    N = N + 1
    ReDim Preserve Tref(1 To N)
    Tref(N) = C
  Next C
  ReDim Nref(1 To N)
Else
    ReDim Preserve Tref(1 To 1)
    Tref(1) = tblo
    ReDim Nref(1 To 1)
End If

For Each C In Plage
  If C.Value <> "" Then
    For i = LBound(Tref) To UBound(Tref)
      If Tref(i) <> "" Then
        If C = Tref(i) Then
          Nref(i) = 1
          Exit For
        End If
      End If
    Next i
  End If
Next C

For i = LBound(Tref) To UBound(Tref)
  Compare_Range_Array = Compare_Range_Array + Nref(i)
Next i

End Function
 

Pièces jointes

  • Compare range array v1.xls
    31 KB · Affichages: 39

Discussions similaires

Réponses
12
Affichages
295
Réponses
1
Affichages
199

Statistiques des forums

Discussions
312 545
Messages
2 089 484
Membres
104 181
dernier inscrit
Mateke