XL 2010 Suppression d'éléments d'un tableau dynamique

Magic_Doctor

XLDnaute Barbatruc
Bonjour,

J'ai 2 tableaux dynamiques qui n'ont pas forcément la même dimension, par ex. :
tab1 = (Pitalugue, Ernestine, Hillary, Gudule, Ursule, Cunégonde, Romina)
tab2 = (Calamity, Ernestine, Ursule, Florencia, Romina)
Je voudrais retirer de "tab1" les éléments communs avec "tab2". Autrement dit, obtenir :
(Pitalugue, Hillary, Gudule, Cunégonde).
Comment s'y prendre (en VBA) ?
 
Dernière édition:

Bernard_XLD

XLDnaute Barbatruc
Membre du Staff
Bonjour Magic_Doctor, le forum

Plusieurs façons de faire, on peut passer par un tableau intermédiaire:
VB:
Sub Modif_Tab1()
Dim Tab1, Tab2, Tab3, x%, y%, z%, test As Boolean
Tab1 = Array("Pitalugue", "Ernestine", "Hillary", "Gudule", "Ursule", "Cunégonde", "Romina")
Tab2 = Array("Ernestine", "Ursule", "Romina")
ReDim Tab3(0)
z = -1
For x = LBound(Tab1) To UBound(Tab1)
    test = False
    For y = LBound(Tab2) To UBound(Tab2)
        If Tab1(x) = Tab2(y) Then test = True: Exit For
    Next y
    If Not test Then
        z = z + 1
        ReDim Preserve Tab3(z)
        Tab3(z) = Tab1(x)
    End If
Next x
If z > -1 Then
    Tab1 = Tab3
    For x = LBound(Tab1) To UBound(Tab1)
        MsgBox "Valeur conservée " & x + 1 & " sur " & UBound(Tab1) - LBound(Tab1) + 1 & " : " & Tab1(x)
    Next x
Else
    Tab1 = Nothing
    MsgBox "Aucun élément ne subsiste dans Tab1", vbOKOnly + vbInformation
End If
End Sub
Bien cordialement, @+
 

Bernard_XLD

XLDnaute Barbatruc
Membre du Staff
re,

on peut aussi passer par un replace si le tableau est bien un array au départ

Code:
Sub Modif_Tab2()
Dim Tab1, Tab2, x%, y%, z%
Tab1 = Array("Pitalugue", "Ernestine", "Hillary", "Gudule", "Ursule", "Cunégonde", "Romina")
Tab2 = Array("Ernestine", "Ursule", "Romina")
Tab1 = Join(Tab1, "|")
y = LBound(Tab2): z = UBound(Tab2)
For x = y To z: Tab1 = Replace(Tab1, "|" & Tab2(x), ""): Tab1 = Replace(Tab1, Tab2(x) & "|", ""): Tab1 = Replace(Tab1, Tab2(x), ""): Next x
Tab1 = Split(Tab1, "|")
MsgBox UBound(Tab1) - LBound(Tab1) + 1 & " valeurs conservées : " & Join(Tab1, ", "), vbOKOnly + vbInformation
End Sub

Bien cordialement, @+
 
Dernière édition:

job75

XLDnaute Barbatruc
Bonjour Magic_Doctor, Yeahou,

Le plus simple si les tableaux ne sont pas très grands ;
VB:
Sub Test()
Dim tab1, tab2, e, n, tab3()
tab1 = Array("Pitalugue", "Ernestine", "Hillary", "Gudule", "Ursule", "Cunégonde", "Romina")
tab2 = Array("Calamity", "Ernestine", "Ursule", "Florencia", "Romina")
For Each e In tab1
    If IsError(Application.Match(e, tab2, 0)) Then
        ReDim Preserve tab3(n)
        tab3(n) = e
        n = n + 1
    End If
Next
'---vérification---
If n Then MsgBox Join(tab3, vbLf) Else MsgBox "Pas de résultat..."
End Sub
Si les tableaux sont grands utiliser le Dictionary sera plus rapide :
VB:
Sub Test()
Dim tab1, tab2, d As Object, e
tab1 = Array("Pitalugue", "Ernestine", "Hillary", "Gudule", "Ursule", "Cunégonde", "Romina")
tab2 = Array("Calamity", "Ernestine", "Ursule", "Florencia", "Romina")
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare 'la casse est ignorée
For Each e In tab1
    d(e) = ""
Next
For Each e In tab2
    If d.exists(e) Then d.Remove e
Next
'---vérification---
If d.Count Then MsgBox Join(d.keys, vbLf) Else MsgBox "Pas de résultat..."
End Sub
A+
 

Statistiques des forums

Discussions
312 393
Messages
2 087 972
Membres
103 688
dernier inscrit
Amadou