comparaison de 2 listes excel et nom commun

ju89

XLDnaute Nouveau
Bonjour,

j'ai trouvé, pour les besoin d'un tableau excel, un bout de code que j'ai réussi à adapté à mon cas.

il s'agit de comparer 2 listes et de renvoyé les noms communs aux deux listes.
La dessus pas de soucis ça fonctionne bien.
Le soucis que j'ai est que si je retire des noms de la liste 2, ils restent tout de même affiché dans les nom commun alors qu'il ne sont plus commun aux 2 liste.

comment faire?

A noté que je n'y connais rien au VBA, j'ai fais de la bidouille a partir de code déjà existant.

voici mon code (trouvé ici: Formation Excel VBA JB ):

Sub Communs()
Set f1 = Sheets("code")
Set f2 = Sheets("mars 2015")
Set mondico1 = CreateObject("Scripting.Dictionary")
For Each c In f1.Range("m5:m15" & f1.[m65000].End(xlUp).Row)
mondico1.Item(c.Value) = c.Value
Next c
Set mondico2 = CreateObject("Scripting.Dictionary")
For Each c In f2.Range("v3:V100" & f2.[d65000].End(xlUp).Row)
If mondico1.Exists(c.Value) Then If Not mondico2.Exists(c.Value) Then mondico2.Add c.Value, c.Value
Next c
Sheets("Mars 2015").[AB5].Resize(mondico2.Count, 1) = Application.Transpose(mondico2.items)
End Sub

Merci pour vos réponses
 

BOISGONTIER

XLDnaute Barbatruc
Repose en paix
Re : comparaison de 2 listes excel et nom commun

Bonjour,

Il faut utiliser une fonction personnalisée matricielle:

http://boisgontierjacques.free.fr/fichiers/fonctionsperso/FonctionCommuns.xls

-Sélectionner un champ vertical
=communs(A2:A12;C2:C12)
-Valider avec maj+ctrl+entrée

Code:
Function Communs(champ1, champ2)
 Dim temp()
 Set MonDico1 = CreateObject("Scripting.Dictionary")
  For Each c In champ1
    If Not MonDico1.Exists(c.Value) Then MonDico1.Add c.Value, c.Value
  Next c
  Set mondico2 = CreateObject("Scripting.Dictionary")
  For Each c In champ2
    If c <> "" And MonDico1.Exists(c.Value) Then
      If Not mondico2.Exists(c.Value) Then mondico2.Add c.Value, c.Value
    End If
  Next c
  i = 1
  ReDim temp(1 To Application.Caller.Rows.Count)
  i = 1
  For Each c In mondico2.items
    temp(i) = c
    i = i + 1
  Next
  Communs = Application.Transpose(temp)
End Function

JB
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 228
Messages
2 086 421
Membres
103 205
dernier inscrit
zch