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 :

Comboxbox Listbox ListView en cascade


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Webdesigner
    Inscrit en
    Janvier 2014
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Janvier 2014
    Messages : 35
    Par défaut
    Bonjour à tous,
    Je pédale un peu dans la semoule car je cherche à faire un truc que je n'arrive pas, c'est pour ça que je viens vers vous.

    Suivant mon fichier ci-joint j'essaie de faire des filtres en cascade via un formulaire (comboxbox1/2/3/lisbox1 fonctionnent en cascade), cependant je n'arrive pas à trouver la suite logique quand je sélectionne 1 ou plusieurs données sur ma listbox1 pour remplir la listbox2, tout en prenant en compte les combobox 1 2 et 3 et listebox1 d'avant (tout ça bien sur en triant sans doublon et par ordre alphabétique)

    Puis après remplir ma listview1 avec toutes les autres données filtrées, pour que cela m'affiche les colonnes AE à AR.

    Voici mon code :
    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
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    '== On déclare la variable public pour la feuille de travail ==
    Dim f
     
    '== CommandButton1 : Reset filter ==
    Private Sub CommandButton1_Click()
    '==== On efface toutes les données
    Me.ComboBox1.Clear
    Me.ComboBox2.Clear
    Me.ComboBox3.Clear
    Me.ListBox1.Clear
    Me.ListBox2.Clear
    '==== On réinitialise l'UserForm
    UserForm_Initialize
    End Sub
     
    '== CommandButton2 : Close UserForm ==
    Private Sub CommandButton2_Click()
    '==== On ferme l'UserForm
    Unload MyUserForm
    End Sub
     
    '== On initialise l'UserFrom ==
    Private Sub UserForm_Initialize()
    '==== On déclare la feuille sur laquelle on travail
    Set f = Sheets("BD")
    '==== On vient sur le ComboxBox1 lors du démarrage
    Me.ComboBox1.SetFocus
    '==== On créé le dictionnaire
    Set mondico = CreateObject("Scripting.Dictionary")
    '==== On déclare la plage pour la ComboBox1
    For Each c In Range("H2:H" & [H65000].End(xlUp).Row)
    mondico(c.Value) = ""
    Next c
    '==== On vient remplir une variable, ici temp, qui vient lister toutes les données
    temp = mondico.keys
    '==== On appelle la fonction Tri pour trier la ComboBox1
    Call Tri(temp, LBound(temp), UBound(temp))
    '==== Une fois trié on remplit la ComboBox1
    Me.ComboBox1.List = temp
    End Sub
     
    '== Lorsque l'on clique sur le ComboBox1, cela rempli la ComboBox2 ==
    Private Sub ComboBox1_click()
      Me.ComboBox2.Clear
      Me.ComboBox3.Clear
      Me.ListBox1.Clear
      Me.ListBox2.Clear
      Set mondico = CreateObject("Scripting.Dictionary")
      For Each c In Range("H2:H" & [H65000].End(xlUp).Row)
        If c = Me.ComboBox1 Then mondico(c.Offset(0, 3).Value) = ""
      Next c
      temp = mondico.keys
      Call Tri(temp, LBound(temp), UBound(temp))
      Me.ComboBox2.List = temp
    End Sub
     
    '== Lorsque l'on clique sur le ComboBox2, cela rempli la ComboBox3 ==
    Private Sub ComboBox2_click()
      Me.ComboBox3.Clear
      Me.ListBox1.Clear
      Me.ListBox2.Clear
      Set mondico = CreateObject("Scripting.Dictionary")
      For Each c In Range("D2:D" & [D65000].End(xlUp).Row)
        If c.Offset(, 4) = Me.ComboBox1 And c.Offset(, 7) = Me.ComboBox2 Then mondico(c.Value) = ""
      Next c
      temp = mondico.keys
      'Call Tri(temp, LBound(temp), UBound(temp))
      Me.ComboBox3.List = temp
    End Sub
     
    '== Lorsque l'on clique sur le ComboBox3, cela rempli la ListBox1 ==
    Private Sub ComboBox3_click()
      Me.ListBox1.Clear
      Me.ListBox2.Clear
      Set mondico = CreateObject("Scripting.Dictionary")
      i = 0
      For Each c In Range("I2:I" & [I65000].End(xlUp).Row)
        If c.Offset(, -1) = Me.ComboBox1 And c.Offset(, 2) = Me.ComboBox2 And c.Offset(, -5).Value = Me.ComboBox3 Then
          mondico(c.Value) = ""
          Me.ListBox1.AddItem c
          i = i + 1
        End If
      Next c
      temp = mondico.keys
      Call Tri(temp, LBound(temp), UBound(temp))
      Me.ListBox1.List = temp
      Me.ListBox1.MultiSelect = fmMultiSelectMulti
    End Sub
     
    '== Lorsque l'on change une donnée dans la ListBox1, cela rempli la ListBox2 ==
    Private Sub ListBox1_Change()
        Me.ListBox2.Clear
        Set mondico = CreateObject("Scripting.Dictionary")
        For Each c In Range(f.[I2], f.[I65000].End(xlUp))
            For k = 0 To Me.ListBox1.ListCount - 1
              If Me.ListBox1.Selected(k) = True Then
                If c = Me.ListBox1.List(k, 0) Then
                  temp = c.Offset(, 1)
                  mondico(temp) = temp
                End If
              End If
            Next k
        Next c
        temp = mondico.keys
        Call Tri(temp, LBound(temp), UBound(temp))
        Me.ListBox2.List = temp
    End Sub
     
    '== Fonction Tri
    Sub Tri(a, gauc, droi)
      ref = a((gauc + droi) \ 2)
      g = gauc: d = droi
      Do
        Do While a(g) < ref: g = g + 1: Loop
        Do While ref < a(d): d = d - 1: Loop
          If g <= d Then
            temp = a(g): a(g) = a(d): a(d) = temp
            g = g + 1: d = d - 1
          End If
        Loop While g <= d
        If g < droi Then Call Tri(a, g, droi)
        If gauc < d Then Call Tri(a, gauc, d)
    End Sub
    Une idée ?
    Fichiers attachés Fichiers attachés
    • Type de fichier : zip BD.zip (1,36 Mo, 146 affichages)

  2. #2
    Membre Expert
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    652
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2009
    Messages : 652
    Par défaut
    Bonjour,

    Tout d'abord, il faut se méfier du contrôle Frame.
    J'ai supprimé le Frame7 de votre UserForm qui empêchait l'usage des ScrollBars dans la ListView1.
    Je l'ai remplacé par un Label1 qui n'est pas un contrôle conteneur (comme l'est Frame) et qui n'interfère pas malencontreusement sur d'autres contrôles. A vous de l'enjoliver.

    D'autre part, j'ai été obligé de monter (par menu Outils/Références) la librairie suivante sinon la ListView1 ne fonctionnait pas :
    Library MSComctlLib C:\WINDOWS\system32\MSCOMCTL.OCX Contrôles communs Microsoft Windows 6.0
    J'ai remplacé dans votre code plusieurs instructions (voir la portion située entre des ########).
    Cela a l'air de fonctionner chez moi.

    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
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    Dim f
     
    '##################################################
    Private Sub ListBox1_Change()
    Dim T()
    Dim cpt&  ' compteur
    '---
    ListBox2.Clear
    ListView1.ListItems.Clear
    '---
    For k = 0 To ListBox1.ListCount - 1
      If Me.ListBox1.Selected(k) = True Then
        For Each c In Range(f.[I2], f.[I65000].End(xlUp))
          If c = ListBox1.List(k, 0) Then
            If c.Offset(0, -1) = ComboBox1 And c.Offset(0, 2) = ComboBox2 And c.Offset(0, -5) = ComboBox3 Then
              cpt& = cpt& + 1
              ReDim Preserve T(1 To 2, 1 To cpt&)
              T(1, cpt&) = c.Offset(, 1)
              T(2, cpt&) = c.Row  'renseigne la 2ème colonne du N° de ligne dans Excel pour pouvoir l'utiliser par la suite
            End If
          End If
        Next c
      End If
    Next k
    '---
    If cpt& = 1 Then
      ListBox2.AddItem T(1, 1)
      ListBox2.List(ListBox2.ListCount - 1, 1) = T(2, 1)
    ElseIf cpt& > 1 Then
      ListBox2.List = Application.WorksheetFunction.Transpose(T)
    Else
      ListView1.ListItems.Clear
    End If
    End Sub
     
    Private Sub ListBox2_Change()
    Dim R As Range
    Dim var
    Dim Lig&
    Dim j&
    Dim k&
    '---
    With ListView1
      .ListItems.Clear
      .View = lvwReport
      .FullRowSelect = True
      .Gridlines = True
      .HideColumnHeaders = False
    End With
    '---
    For k& = 0 To ListBox2.ListCount - 1
     
      '/// C'est ic qu'on utilise le N° de ligne dans Excel ///
      Lig& = ListBox2.List(k&, 1)
      Set R = f.Range("AE" & Lig& & ":AR" & Lig& & "")
      var = R
      '////////////////////////////////////////////////////////
     
      With ListView1
        If var(1, 1) = "" Then var(1, 1) = "na"
          .ListItems.Add , , var(1, 1)
          For j& = 2 To UBound(var, 2)
            If var(1, j&) = "" Then var(1, j&) = "na"
            .ListItems(.ListItems.Count).ListSubItems.Add , , var(1, j&)
          Next j&
      End With
    Next k&
    End Sub
     
    Private Sub UserForm_Initialize()
      Set f = Sheets("BD")
      Me.ComboBox1.SetFocus
      Set mondico = CreateObject("Scripting.Dictionary")
      For Each c In Range("H2:H" & [H65000].End(xlUp).Row)
        mondico(c.Value) = ""
      Next c
      temp = mondico.keys
      Call Tri(temp, LBound(temp), UBound(temp))
      Me.ComboBox1.List = temp
      '---
      With Me.ListBox2
        .BoundColumn = 1
        .ColumnCount = 2
        .ColumnWidths = "1cm;0cm"   'la 2ème colonne est cachée (0cm)
      End With
      '--- Titres de ListView1 ---
      With Me.ListView1
        .HideColumnHeaders = True
        With .ColumnHeaders
          .Add , , "1st T200", 45, lvwColumnLeft
          .Add , , "1st DMU3", 45, 2
          .Add , , "1st T500", 45, 2
          .Add , , "1st Relea", 45, 2
          .Add , , "Act T200", 45, 2
          .Add , , "Act DMU3", 45, 2
          .Add , , "Act T500", 45, 2
          .Add , , "Act Relea", 45, 2
          .Add , , "eS T100", 45, 2
          .Add , , "eS T200", 45, 2
          .Add , , "eS T400", 45, 2
          .Add , , "eS T500", 45, 2
          .Add , , "eS T700", 45, 2
          .Add , , "eS Need", 45, 2
        End With
    End With
    End Sub
    '##################################################
     
    Private Sub CommandButton1_Click()
    Me.ComboBox1.Clear
    Me.ComboBox2.Clear
    Me.ComboBox3.Clear
    Me.ListBox1.Clear
    Me.ListBox2.Clear
    UserForm_Initialize
    End Sub
     
    Private Sub CommandButton2_Click()
    Unload MyUserForm
    End Sub
     
    Private Sub ComboBox1_click()
      Me.ComboBox2.Clear
      Me.ComboBox3.Clear
      Me.ListBox1.Clear
      Me.ListBox2.Clear
      Set mondico = CreateObject("Scripting.Dictionary")
      For Each c In Range("H2:H" & [H65000].End(xlUp).Row)
        If c = Me.ComboBox1 Then mondico(c.Offset(0, 3).Value) = ""
      Next c
      temp = mondico.keys
      Call Tri(temp, LBound(temp), UBound(temp))
      Me.ComboBox2.List = temp
    End Sub
    Private Sub ComboBox2_click()
      Me.ComboBox3.Clear
      Me.ListBox1.Clear
      Me.ListBox2.Clear
      Set mondico = CreateObject("Scripting.Dictionary")
      For Each c In Range("D2:D" & [D65000].End(xlUp).Row)
        If c.Offset(, 4) = Me.ComboBox1 And c.Offset(, 7) = Me.ComboBox2 Then mondico(c.Value) = ""
      Next c
      temp = mondico.keys
      Call Tri(temp, LBound(temp), UBound(temp))
      Me.ComboBox3.List = temp
    End Sub
    Private Sub ComboBox3_click()
      Me.ListBox1.Clear
      Me.ListBox2.Clear
      Set mondico = CreateObject("Scripting.Dictionary")
      i = 0
      For Each c In Range("I2:I" & [I65000].End(xlUp).Row)
        If c.Offset(, -1) = Me.ComboBox1 And c.Offset(, 2) = Me.ComboBox2 And c.Offset(, -5).Value = Me.ComboBox3 Then
          mondico(c.Value) = ""
          Me.ListBox1.AddItem c
          i = i + 1
        End If
      Next c
      temp = mondico.keys
      Call Tri(temp, LBound(temp), UBound(temp))
      Me.ListBox1.List = temp
      Me.ListBox1.MultiSelect = fmMultiSelectMulti
    End Sub
    Sub Tri(a, gauc, droi) ' Quick sort
      ref = a((gauc + droi) \ 2)
      g = gauc: d = droi
      Do
        Do While a(g) < ref: g = g + 1: Loop
        Do While ref < a(d): d = d - 1: Loop
          If g <= d Then
            temp = a(g): a(g) = a(d): a(d) = temp
            g = g + 1: d = d - 1
          End If
        Loop While g <= d
        If g < droi Then Call Tri(a, g, droi)
        If gauc < d Then Call Tri(a, gauc, d)
    End Sub
    Fichiers attachés Fichiers attachés

  3. #3
    Membre averti
    Homme Profil pro
    Webdesigner
    Inscrit en
    Janvier 2014
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Janvier 2014
    Messages : 35
    Par défaut
    Yes merci pour ton fichier, ça commence vraiment à être pas mal.

    Du coup je suis un peu perdu pour supprimer les doublons dans la listbox2, comment faire ?

    PS : avec excel 2013 et windows 10 j'arrive pas à faire fonctionner le fichier.
    Fichiers attachés Fichiers attachés

Discussions similaires

  1. ListBox, ListView ou Datagridview
    Par olibara dans le forum C#
    Réponses: 3
    Dernier message: 12/08/2012, 18h15
  2. MVVM ListBox/ListView Selected Item
    Par gridin dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 03/02/2011, 09h51
  3. listbox en cascade
    Par meumeu73.1 dans le forum Macros et VBA Excel
    Réponses: 11
    Dernier message: 23/01/2008, 15h05
  4. 3 Combobox (Listbox) en cascade la 3éme colonne ne s'affiche pas
    Par minogttao dans le forum Général JavaScript
    Réponses: 8
    Dernier message: 08/11/2006, 22h44
  5. [C#] ListBox, ListView, TreeView
    Par Kerod dans le forum Windows Forms
    Réponses: 1
    Dernier message: 31/05/2006, 17h32

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