Clo from Ghost World
XLDnaute Nouveau
Bonjour à tous,
Je viens de créer un UserForm "recherche" avec plusieurs critères (nom, prénom, taille, poids, cheveux...).
Or, un problème se pose à moi. J'aimerais qu'un de mes ComboBox "Compétences" puisse faire une recherche dans 4 colonnes ("Compétence 1", "compétence 2", "compétence 3", compétence 4") de ma base de données et non une seule.
Pour avoir essayé de questionner le problème, il semblerait que le code devrait être à modifier quand on définit les Initcombo, cad à ce niveau là :
-----------------------
Call InitCombo(ComboBox1, "B")
Call InitCombo(ComboBox2, "C")
Call InitCombo(ComboBox3, "N")
Call InitCombo(ComboBox4, "O")
Call InitCombo(ComboBox5, "Q")
Call InitCombo(ComboBox6, "P")
Call InitCombo(ComboBox7, "R")
Call InitCombo(ComboBox8, "T")
Call InitCombo(ComboBox9, "v")
Call InitCombo(ComboBox10, "S")
------------------------
Et également à ce niveau :
------------------------
' Boucle de la 2me à la dernière ligne de la feuille Feuil1
For lgLigDeb = 6 To Range("b" & Cells.Rows.Count).End(xlUp).Row
If Range("b" & lgLigDeb).Value Like Critere1 And Range("c" & lgLigDeb).Value Like Critere2 And Range("n" & lgLigDeb).Value Like Critere3 And Range("o" & lgLigDeb).Value Like Critere4 And Range("Q" & lgLigDeb).Value Like Critere5 And Range("P" & lgLigDeb).Value Like Critere6 And Range("R" & lgLigDeb).Value Like Critere7 And Range("T" & lgLigDeb).Value Like Critere8 And Range("V" & lgLigDeb).Value Like Critere9 And Range("S" & lgLigDeb).Value Like Critere10 Then
-------------------------
Mais mes maigres connaissances m'empêchent d'y voir plus clair. Si quelqu'un peut donc m'apporter un peu de sa lumière pour m'aider à résoudre ce problème, je l'en remercie.
Voici mon code au complet :
--------------------------
Private Sub UserForm_Initialize()
Range("b6").Select
' Initialiser les listes des critères
Call InitCombo(ComboBox1, "B")
Call InitCombo(ComboBox2, "C")
Call InitCombo(ComboBox3, "N")
Call InitCombo(ComboBox4, "O")
Call InitCombo(ComboBox5, "Q")
Call InitCombo(ComboBox6, "P")
Call InitCombo(ComboBox7, "R")
Call InitCombo(ComboBox8, "T")
Call InitCombo(ComboBox9, "v")
Call InitCombo(ComboBox10, "S")
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox1_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox2_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox3_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox4_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox5_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox6_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox7_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox8_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox9_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox10_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub Rechercher()
' Rechercher les données en fonction des critères 1 et 2
Dim rCel As Range
Dim lgLig As Long
Dim lgLigDeb As Long
Dim Critere1 As String
Dim Critere2 As String
Dim Critere3 As String
Dim Critere4 As String
Dim Critere5 As String
Dim Critere6 As String
Dim Critere7 As String
Dim Critere8 As String
Dim Critere9 As String
Dim Critere10 As String
Critere1 = "*"
If ComboBox1.Value <> "" Then Critere1 = ComboBox1.Value
Critere2 = "*"
If ComboBox2.Value <> "" Then Critere2 = ComboBox2.Value
Critere3 = "*"
If ComboBox3.Value <> "" Then Critere3 = ComboBox3.Value
Critere4 = "*"
If ComboBox4.Value <> "" Then Critere4 = ComboBox4.Value
Critere5 = "*"
If ComboBox5.Value <> "" Then Critere5 = ComboBox5.Value
Critere6 = "*"
If ComboBox6.Value <> "" Then Critere6 = ComboBox6.Value
Critere7 = "*"
If ComboBox7.Value <> "" Then Critere7 = ComboBox7.Value
Critere8 = "*"
If ComboBox8.Value <> "" Then Critere8 = ComboBox8.Value
Critere9 = "*"
If ComboBox9.Value <> "" Then Critere9 = ComboBox9.Value
Critere10 = "*"
If ComboBox10.Value <> "" Then Critere10 = ComboBox10.Value
ListBox1.Clear
' Boucle de la 2me à la dernière ligne de la feuille Feuil1
For lgLigDeb = 6 To Range("b" & Cells.Rows.Count).End(xlUp).Row
If Range("b" & lgLigDeb).Value Like Critere1 And Range("c" & lgLigDeb).Value Like Critere2 And Range("n" & lgLigDeb).Value Like Critere3 And Range("o" & lgLigDeb).Value Like Critere4 And Range("Q" & lgLigDeb).Value Like Critere5 And Range("P" & lgLigDeb).Value Like Critere6 And Range("R" & lgLigDeb).Value Like Critere7 And Range("T" & lgLigDeb).Value Like Critere8 And Range("V" & lgLigDeb).Value Like Critere9 And Range("S" & lgLigDeb).Value Like Critere10 Then
With ListBox1
.AddItem Range("A" & lgLigDeb).Value
.List(.ListCount - 1, 1) = Range("C" & lgLigDeb).Value
.List(.ListCount - 1, 2) = Range("B" & lgLigDeb).Value
.List(.ListCount - 1, 3) = lgLigDeb
lgLig = lgLig + 1
End With
End If
Next lgLigDeb
End Sub
Private Sub InitCombo(LCombo As Object, nomCol As String)
Dim lig As Long
Dim nbElement As Integer
Dim trouveElm As Boolean
LCombo.Clear
' Boucle de la ligne 2 à la dernière ligne dans la colonne nomCol
For lig = 6 To Range(nomCol & Cells.Rows.Count).End(xlUp).Row
trouveElm = False
' Vérifier que l'élément à ajouter dans la liste n'existe pas déjà
For nbElement = 0 To LCombo.ListCount - 1
' L'élément est déjà présent dans la liste, sortie de la boucle
If LCombo.List(nbElement) = Range(nomCol & lig).Value Then
trouveElm = True
Exit For
End If
Next nbElement
' Elément non trouvé dans la liste, l'ajouter
If trouveElm = False Then LCombo.AddItem Range(nomCol & lig).Value
Next lig
End Sub
----------------------------
Je viens de créer un UserForm "recherche" avec plusieurs critères (nom, prénom, taille, poids, cheveux...).
Or, un problème se pose à moi. J'aimerais qu'un de mes ComboBox "Compétences" puisse faire une recherche dans 4 colonnes ("Compétence 1", "compétence 2", "compétence 3", compétence 4") de ma base de données et non une seule.
Pour avoir essayé de questionner le problème, il semblerait que le code devrait être à modifier quand on définit les Initcombo, cad à ce niveau là :
-----------------------
Call InitCombo(ComboBox1, "B")
Call InitCombo(ComboBox2, "C")
Call InitCombo(ComboBox3, "N")
Call InitCombo(ComboBox4, "O")
Call InitCombo(ComboBox5, "Q")
Call InitCombo(ComboBox6, "P")
Call InitCombo(ComboBox7, "R")
Call InitCombo(ComboBox8, "T")
Call InitCombo(ComboBox9, "v")
Call InitCombo(ComboBox10, "S")
------------------------
Et également à ce niveau :
------------------------
' Boucle de la 2me à la dernière ligne de la feuille Feuil1
For lgLigDeb = 6 To Range("b" & Cells.Rows.Count).End(xlUp).Row
If Range("b" & lgLigDeb).Value Like Critere1 And Range("c" & lgLigDeb).Value Like Critere2 And Range("n" & lgLigDeb).Value Like Critere3 And Range("o" & lgLigDeb).Value Like Critere4 And Range("Q" & lgLigDeb).Value Like Critere5 And Range("P" & lgLigDeb).Value Like Critere6 And Range("R" & lgLigDeb).Value Like Critere7 And Range("T" & lgLigDeb).Value Like Critere8 And Range("V" & lgLigDeb).Value Like Critere9 And Range("S" & lgLigDeb).Value Like Critere10 Then
-------------------------
Mais mes maigres connaissances m'empêchent d'y voir plus clair. Si quelqu'un peut donc m'apporter un peu de sa lumière pour m'aider à résoudre ce problème, je l'en remercie.
Voici mon code au complet :
--------------------------
Private Sub UserForm_Initialize()
Range("b6").Select
' Initialiser les listes des critères
Call InitCombo(ComboBox1, "B")
Call InitCombo(ComboBox2, "C")
Call InitCombo(ComboBox3, "N")
Call InitCombo(ComboBox4, "O")
Call InitCombo(ComboBox5, "Q")
Call InitCombo(ComboBox6, "P")
Call InitCombo(ComboBox7, "R")
Call InitCombo(ComboBox8, "T")
Call InitCombo(ComboBox9, "v")
Call InitCombo(ComboBox10, "S")
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox1_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox2_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox3_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox4_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox5_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox6_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox7_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox8_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox9_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub ComboBox10_Change()
' Rechercher les données en fonction des critères sélectionnés
Call Rechercher
End Sub
Private Sub Rechercher()
' Rechercher les données en fonction des critères 1 et 2
Dim rCel As Range
Dim lgLig As Long
Dim lgLigDeb As Long
Dim Critere1 As String
Dim Critere2 As String
Dim Critere3 As String
Dim Critere4 As String
Dim Critere5 As String
Dim Critere6 As String
Dim Critere7 As String
Dim Critere8 As String
Dim Critere9 As String
Dim Critere10 As String
Critere1 = "*"
If ComboBox1.Value <> "" Then Critere1 = ComboBox1.Value
Critere2 = "*"
If ComboBox2.Value <> "" Then Critere2 = ComboBox2.Value
Critere3 = "*"
If ComboBox3.Value <> "" Then Critere3 = ComboBox3.Value
Critere4 = "*"
If ComboBox4.Value <> "" Then Critere4 = ComboBox4.Value
Critere5 = "*"
If ComboBox5.Value <> "" Then Critere5 = ComboBox5.Value
Critere6 = "*"
If ComboBox6.Value <> "" Then Critere6 = ComboBox6.Value
Critere7 = "*"
If ComboBox7.Value <> "" Then Critere7 = ComboBox7.Value
Critere8 = "*"
If ComboBox8.Value <> "" Then Critere8 = ComboBox8.Value
Critere9 = "*"
If ComboBox9.Value <> "" Then Critere9 = ComboBox9.Value
Critere10 = "*"
If ComboBox10.Value <> "" Then Critere10 = ComboBox10.Value
ListBox1.Clear
' Boucle de la 2me à la dernière ligne de la feuille Feuil1
For lgLigDeb = 6 To Range("b" & Cells.Rows.Count).End(xlUp).Row
If Range("b" & lgLigDeb).Value Like Critere1 And Range("c" & lgLigDeb).Value Like Critere2 And Range("n" & lgLigDeb).Value Like Critere3 And Range("o" & lgLigDeb).Value Like Critere4 And Range("Q" & lgLigDeb).Value Like Critere5 And Range("P" & lgLigDeb).Value Like Critere6 And Range("R" & lgLigDeb).Value Like Critere7 And Range("T" & lgLigDeb).Value Like Critere8 And Range("V" & lgLigDeb).Value Like Critere9 And Range("S" & lgLigDeb).Value Like Critere10 Then
With ListBox1
.AddItem Range("A" & lgLigDeb).Value
.List(.ListCount - 1, 1) = Range("C" & lgLigDeb).Value
.List(.ListCount - 1, 2) = Range("B" & lgLigDeb).Value
.List(.ListCount - 1, 3) = lgLigDeb
lgLig = lgLig + 1
End With
End If
Next lgLigDeb
End Sub
Private Sub InitCombo(LCombo As Object, nomCol As String)
Dim lig As Long
Dim nbElement As Integer
Dim trouveElm As Boolean
LCombo.Clear
' Boucle de la ligne 2 à la dernière ligne dans la colonne nomCol
For lig = 6 To Range(nomCol & Cells.Rows.Count).End(xlUp).Row
trouveElm = False
' Vérifier que l'élément à ajouter dans la liste n'existe pas déjà
For nbElement = 0 To LCombo.ListCount - 1
' L'élément est déjà présent dans la liste, sortie de la boucle
If LCombo.List(nbElement) = Range(nomCol & lig).Value Then
trouveElm = True
Exit For
End If
Next nbElement
' Elément non trouvé dans la liste, l'ajouter
If trouveElm = False Then LCombo.AddItem Range(nomCol & lig).Value
Next lig
End Sub
----------------------------
Dernière édition: