IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Supprimer lignes vides d'un tableau


Sujet :

Macros et VBA Excel

  1. #21
    Nouveau membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Novembre 2014
    Messages
    39
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Bâtiment

    Informations forums :
    Inscription : Novembre 2014
    Messages : 39
    Points : 29
    Points
    29
    Par défaut
    Bonjour,

    Petite question supplémentaire,

    Qu'elle méthode est la plus simple pour trier la colonne 1 de ce tableau (ordre des distances des plus petites aux plus grandes) tout en conservant la correspondance de la colonne 2?

    Je m'oriente vers le Quick sort? Y a-t-il plus simple?

  2. #22
    Membre éprouvé
    Homme Profil pro
    Directeur
    Inscrit en
    Avril 2003
    Messages
    724
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Avril 2003
    Messages : 724
    Points : 1 166
    Points
    1 166
    Par défaut
    L'ArrayList possède une méthode Sort.
    Essaie
    EDIT

    Non, je viens d'essayer, ça ne marche pas.
    Il faut trier le tableau avec QuickSort.

  3. #23
    Membre chevronné
    Homme Profil pro
    Inscrit en
    Septembre 2013
    Messages
    1 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2013
    Messages : 1 369
    Points : 2 156
    Points
    2 156
    Par défaut
    Bonjour,

    Exemple

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    Option Compare Text
    Private Sub UserForm_Initialize()
     With Sheets("BD")
       a = .Range("A2:C" & .[A65000].End(xlUp).Row).Value
     End With
     Dim tmp(): ReDim tmp(1 To UBound(a))
      For i = LBound(a) To UBound(a)      ' sup lignes vides de a(,)
        If a(i, 1) <> "" Then n = n + 1: tmp(n) = i
      Next
      ReDim Preserve tmp(1 To n)
      a = Application.Index(a, Application.Transpose(tmp), _
        Application.Transpose(Evaluate("Row(1:" & UBound(a, 2) & ")")))
      Call tri(a, LBound(a), UBound(a), 1)
      Me.ListBox1.List = a
    End Sub
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    Sub tri(a, gauc, droi, colTri)             ' Quick sort
     NbCol = UBound(a, 2) - LBound(a, 2) + 1   ' nb de colonnes
     ref = a((gauc + droi) \ 2, colTri)
     g = gauc: d = droi
     Do
         Do While a(g, colTri) < ref: g = g + 1: Loop
         Do While ref < a(d, colTri): d = d - 1: Loop
         If g <= d Then
           For c = 1 To NbCol
             temp = a(g, c): a(g, c) = a(d, c): a(d, c) = temp
           Next
           g = g + 1: d = d - 1
         End If
     Loop While g <= d
     If g < droi Then Call tri(a, g, droi, colTri)
     If gauc < d Then Call tri(a, gauc, d, colTri)
    End Sub
    ou avec ArrayList

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    Option Compare Text
    Private Sub UserForm_Initialize()
     With Sheets("BD")
       a = .Range("A2:C" & .[A65000].End(xlUp).Row).Value
     End With
     Set AL = CreateObject("System.Collections.ArrayList")
     For i = LBound(a) To UBound(a)
         If a(i, 1) <> "" Then AL.Add Array(a(i, 1), a(i, 2), a(i, 3))
     Next i
     a = Application.Transpose(Application.Transpose(AL.toarray))
     Call tri(a, LBound(a), UBound(a), 1)
     Me.ListBox1.List = a
    End Sub

    Boisgontier
    http://boisgontierjacques.free.fr
    Fichiers attachés Fichiers attachés

  4. #24
    Nouveau membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Novembre 2014
    Messages
    39
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Bâtiment

    Informations forums :
    Inscription : Novembre 2014
    Messages : 39
    Points : 29
    Points
    29
    Par défaut
    Merci à vous Philippe et Jacques!

    Le quick sort fonctionne parfaitement!

    Une méthode très ingénieuse!

  5. #25
    Membre chevronné
    Homme Profil pro
    Inscrit en
    Septembre 2013
    Messages
    1 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2013
    Messages : 1 369
    Points : 2 156
    Points
    2 156
    Par défaut
    Bonjour,

    Suppression de lignes vides dans ListBox triéavec Dictionary (0,2 sec pour 10.000 lignes contre 0,5 sec avec ArrayList)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    Option Compare Text
    Private Sub UserForm_Initialize()
     Set f = Sheets("BD")
     Set d = CreateObject("Scripting.Dictionary")
     a = f.Range("A2:C" & f.[A65000].End(xlUp).Row).Value
     For i = LBound(a) To UBound(a)
       If a(i, 1) <> "" Then d(i) = Array(a(i, 1), a(i, 2), a(i, 3))
     Next i
     a = Application.Transpose(Application.Transpose(d.items))
     Call tri(a, LBound(a), UBound(a), 1)
     Me.ListBox1.List = a
    End Sub
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    Sub tri(a, gauc, droi, colTri)             ' Quick sort
     ref = a((gauc + droi) \ 2, colTri)
     g = gauc: d = droi
     Do
         Do While a(g, colTri) < ref: g = g + 1: Loop
         Do While ref < a(d, colTri): d = d - 1: Loop
         If g <= d Then
           For c = LBound(a, 2) To UBound(a, 2)
             temp = a(g, c): a(g, c) = a(d, c): a(d, c) = temp
           Next
           g = g + 1: d = d - 1
         End If
     Loop While g <= d
     If g < droi Then Call tri(a, g, droi, colTri)
     If gauc < d Then Call tri(a, gauc, d, colTri)
    End Sub
    Boisgontier
    Fichiers attachés Fichiers attachés

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. [WD-2003] Supprimer ligne vide dans tableau
    Par ginaub00 dans le forum Word
    Réponses: 1
    Dernier message: 27/05/2010, 17h09
  2. [XL-2003] (Cacher) modifié en : Supprimer les lignes vides dans un tableau
    Par azerty1956 dans le forum Macros et VBA Excel
    Réponses: 12
    Dernier message: 15/04/2009, 18h12
  3. Réponses: 4
    Dernier message: 25/11/2008, 09h46
  4. Réponses: 10
    Dernier message: 10/10/2008, 13h22
  5. Réponses: 1
    Dernier message: 12/12/2006, 12h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo