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

Contribuez Discussion :

fonction pour filtrage avec des combobox en cascade


Sujet :

Contribuez

  1. #1
    Membre éprouvé
    Avatar de Montor
    Homme Profil pro
    Autre
    Inscrit en
    Avril 2008
    Messages
    879
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : Autre

    Informations forums :
    Inscription : Avril 2008
    Messages : 879
    Points : 963
    Points
    963
    Par défaut fonction pour filtrage avec des combobox en cascade
    fonction pour filtrage avec des combobox en cascade
    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
     
    Function SetList(this As ComboBox, ParamArray params() As Variant)
    Dim sCol As New Collection, stmps As String
    Dim j As Long, sRow As Long, b As Long
    Dim zt As Integer
    Dim tp As Byte, paramid As Byte
    Dim Refs As Byte
    Dim elem As Variant
    Dim setfind As Boolean
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        sRow = Range("A" & Rows.Count).End(xlUp).Row
        Refs = 100
        setfind = True
        paramid = UBound(params)
        ReDim Tableau(Refs)
        For b = 0 To sRow Step Refs
            Tableau = Range("A1:D" & Refs).Offset(b, 0).Value
            For zt = 1 To Refs
                setfind = True
                For tp = 1 To paramid
                    If (params(tp) <> Trim(Tableau(zt, tp)) And params(tp) <> "*") Then
                        setfind = False
                        Exit For
                    End If
                Next
                If setfind Then
                    stmps = Trim(Tableau(zt, paramid + 1))
                    If stmps <> "" Then
                        On Error Resume Next
                        sCol.Add stmps, CStr(stmps)
                        Err.Clear
                    End If
                End If
            Next
        Next
        If sCol.Count > 0 Then
            ReDim ss(sCol.Count - 1, 0): j = 0
            For Each elem In sCol
                ss(j, 0) = elem
                j = j + 1
            Next
            this.List = ss
        End If
        SetList = sCol.Count
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Function

    exemple avec quatre combobox
    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
     
     
    Option Explicit
    Function SetList(this As ComboBox, ParamArray params() As Variant)
    Dim sCol As New Collection, stmps As String
    Dim j As Long, sRow As Long, b As Long
    Dim zt As Integer
    Dim tp As Byte, paramid As Byte
    Dim Refs As Byte
    Dim elem As Variant
    Dim setfind As Boolean
    With Application
            .ScreenUpdating = False
            .EnableEvents = False
    End With
    sRow = Range("A" & Rows.Count).End(xlUp).Row
    Refs = 100
    setfind = True
    paramid = UBound(params)
    ReDim Tableau(Refs)
    For b = 0 To sRow Step Refs
    Tableau = Range("A1:D" & Refs).Offset(b, 0).Value
    For zt = 1 To Refs
    setfind = True
    For tp = 1 To paramid
    If (params(tp) <> Trim(Tableau(zt, tp)) And params(tp) <> "*") Then
    setfind = False
    Exit For
    End If
    Next
    If setfind Then
    stmps = Trim(Tableau(zt, paramid + 1))
    If stmps <> "" Then
    On Error Resume Next
    sCol.Add stmps, CStr(stmps)
    Err.Clear
    End If
    End If
    Next
    Next
    If sCol.Count > 0 Then
    ReDim ss(sCol.Count - 1, 0): j = 0
    For Each elem In sCol
    ss(j, 0) = elem
    j = j + 1
    Next
    this.List = ss
    End If
    SetList = sCol.Count
    With Application
            .ScreenUpdating = True
            .EnableEvents = True
    End With
    End Function
    Private Sub UserForm_Initialize()
    If SetList(ComBox1, "") > 1 Then ComBox1.AddItem "*"
    End Sub
    Private Sub ComBox1_Change()
    ComBox2.Clear
    If SetList(ComBox2, "", ComBox1.Value) > 1 Then
    ComBox2.AddItem "*"
    End If
    ComBox2_Change
    End Sub
    Private Sub ComBox2_Change()
    ComBox3.Clear
    If SetList(ComBox3, "", ComBox1.Value, ComBox2.Value) > 1 Then
    ComBox3.AddItem "*"
    End If
    ComBox3_Change
    End Sub
    Private Sub ComBox3_Change()
    ComBox4.Clear
    Call SetList(ComBox4, "", ComBox1.Value, ComBox2.Value, ComBox3.Value)
    End Sub

    voici le fichier

  2. #2
    Membre éprouvé
    Avatar de Montor
    Homme Profil pro
    Autre
    Inscrit en
    Avril 2008
    Messages
    879
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : Autre

    Informations forums :
    Inscription : Avril 2008
    Messages : 879
    Points : 963
    Points
    963
    Par défaut
    Pour eviter chaque fois utilisation de "Combobox.Clear" je les inclus dans la fonction
    this.Clear
    this.Text = ""
    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
     
    Function SetList(this As ComboBox, _
                                S_Top As Integer, _
                                S_Left As Integer, _
                                S_Fl As Byte, _
                                Feuille As String, _
                                ParamArray params() As Variant)
                                'this       : ComboBox de sortie
                                'S_Top : ligne de debut de plage
                                'S_Left : colonne de debut de plage
                                'S_Fl    : nombre de champs
                                'Feuille : nom de  Feuille
                                'params: reqete
    Dim sCol As New Collection, stmps As String
    Dim j As Long, sRow As Long
    Dim b As Long
    Dim tops As Integer, lefts As Integer
    Dim zt As Integer
    Dim tp As Integer, paramid As Integer
    Dim Refs As Byte, p As Byte
    Dim elem As Variant
    Dim setfind As Boolean
    With Worksheets(Feuille)
    this.Clear
    this.Text = ""
    sRow = .Cells(Rows.Count, S_Left).End(xlUp).Row
    Refs = 80
    setfind = True
    tops = S_Top + Refs
    lefts = S_Left + (S_Fl - 1)
    If IsMissing(params) Then
    paramid = -1
    Else
    paramid = UBound(params)
    End If
    ReDim Tableau(Refs)
    For b = S_Top - 1 To sRow Step Refs
    Tableau = .Range(.Cells(S_Top, S_Left), .Cells(tops, lefts)).Offset(b, 0).Value
    For zt = 1 To Refs
    setfind = True
    For tp = 0 To paramid
    If (params(tp) <> Tableau(zt, tp + 1) And params(tp) <> "*") Then
    setfind = False
    Exit For
    End If
    Next
    If setfind Then
    stmps = Tableau(zt, paramid + 2)
    If stmps <> "" Then
    On Error Resume Next
    sCol.Add stmps, CStr(stmps)
    Err.Clear
    End If
    End If
    Next
    Next
    End With
    If sCol.Count > 0 Then
    ReDim ss(sCol.Count - 1, 0)
    j = 0
    For Each elem In sCol
    ss(j, 0) = elem
    j = j + 1
    Next
    this.List = ss
    SetList = sCol.Count
    Else
    SetList = 0
    End If
    End Function
    le meme 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
    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
     
    Option Explicit
    Function SetList(this As ComboBox, _
                                S_Top As Integer, _
                                S_Left As Integer, _
                                S_Fl As Byte, _
                                Feuille As String, _
                                ParamArray params() As Variant)
                                'this       : ComboBox de sortie
                                'S_Top : ligne de debut de plage
                                'S_Left : colonne de debut de plage
                                'S_Fl    : nombre de champs
                                'Feuille : nom de  Feuille
                                'params: reqete
    Dim sCol As New Collection, stmps As String
    Dim j As Long, sRow As Long
    Dim b As Long
    Dim tops As Integer, lefts As Integer
    Dim zt As Integer
    Dim tp As Integer, paramid As Integer
    Dim Refs As Byte, p As Byte
    Dim elem As Variant
    Dim setfind As Boolean
    With Worksheets(Feuille)
    this.Clear
    this.Text = ""
    sRow = .Cells(Rows.Count, S_Left).End(xlUp).Row
    Refs = 80
    setfind = True
    tops = S_Top + Refs
    lefts = S_Left + (S_Fl - 1)
    If IsMissing(params) Then
    paramid = -1
    Else
    paramid = UBound(params)
    End If
    ReDim Tableau(Refs)
    For b = S_Top - 1 To sRow Step Refs
    Tableau = .Range(.Cells(S_Top, S_Left), .Cells(tops, lefts)).Offset(b, 0).Value
    For zt = 1 To Refs
    setfind = True
    For tp = 0 To paramid
    If (params(tp) <> Tableau(zt, tp + 1) And params(tp) <> "*") Then
    setfind = False
    Exit For
    End If
    Next
    If setfind Then
    stmps = Tableau(zt, paramid + 2)
    If stmps <> "" Then
    On Error Resume Next
    sCol.Add stmps, CStr(stmps)
    Err.Clear
    End If
    End If
    Next
    Next
    End With
    If sCol.Count > 0 Then
    ReDim ss(sCol.Count - 1, 0)
    j = 0
    For Each elem In sCol
    ss(j, 0) = elem
    j = j + 1
    Next
    this.List = ss
    SetList = sCol.Count
    Else
    SetList = 0
    End If
    End Function
    Private Sub UserForm_Initialize()
    If SetList(ComBox1, 1, 1, 4, "Feuil1") > 1 Then ComBox1.AddItem "*"
    End Sub
    Private Sub ComBox1_Change()
    If SetList(ComBox2, 1, 1, 4, "Feuil1", ComBox1.Value) > 1 Then
    ComBox2.AddItem "*"
    End If
    ComBox2_Change
    End Sub
    Private Sub ComBox2_Change()
    If SetList(ComBox3, 1, 1, 4, "Feuil1", ComBox1.Value, ComBox2.Value) > 1 Then
    ComBox3.AddItem "*"
    End If
    ComBox3_Change
    End Sub
    Private Sub ComBox3_Change()
    Call SetList(ComBox4, 1, 1, 4, "Feuil1", ComBox1.Value, ComBox2.Value, ComBox3.Value)
    End Sub

  3. #3
    Nouveau Candidat au Club
    Inscrit en
    Janvier 2009
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Janvier 2009
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Avec le contrôle ListBox
    Bonjour,


    Merci pour ton code très pratique.

    J'ai essayé de l'adapter à des ListBox en remplaçant simplement Combox par ListBox mais j'ai un message d'erreur.

    Y a t-il d'autres changement à faire ? Peux-tu réécrire ton code avec des ListBox ?

    Merci d'avance pour ta réponse

  4. #4
    Membre confirmé
    Inscrit en
    Janvier 2008
    Messages
    467
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 467
    Points : 493
    Points
    493
    Par défaut
    voici le fichier
    Où se trouve ce fichier ?

Discussions similaires

  1. Réponses: 2
    Dernier message: 27/10/2007, 10h16
  2. changer un textarea par fonction JS : texte avec des sauts de ligne
    Par agrotic dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 25/10/2006, 14h08
  3. Utilisation du Enable avec des combobox ou des listbox
    Par legos dans le forum VB 6 et antérieur
    Réponses: 16
    Dernier message: 19/09/2006, 22h35
  4. [VBA] Requête + date pour travailler avec des recordsets
    Par snoopy69 dans le forum Requêtes et SQL.
    Réponses: 6
    Dernier message: 14/08/2006, 15h15
  5. Réponses: 7
    Dernier message: 04/06/2006, 17h00

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