Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

Gestion de filtre suivant valeur Combobox

  • Initiateur de la discussion Initiateur de la discussion Minus
  • Date de début Date de début

Minus

XLDnaute Nouveau
Type : Microsoft office Excel 2003

Bonjour,

Je suis certain qu'il y a moyen de simplifier cette macro. Etant novice, je fais appel à vos connaissances.

J'ai testé avec des variables... mais je ne suis pas un expert.

En vous remerciant,



Private Sub ComboBox4_Change()

ListBox1.Clear

If ComboBox4.Text = "" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
End If

If ComboBox4.Text = "7" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=7"
End If

If ComboBox4.Text = "8" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=8"
End If

If ComboBox4.Text = "9" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=9"
End If

If ComboBox4.Text = "10" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=10"
End If

If ComboBox4.Text = "11" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=11"
End If

If ComboBox4.Text = "12" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=12"
End If

If ComboBox4.Text = "13" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=13"
End If

If ComboBox4.Text = "14" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=14"
End If

If ComboBox4.Text = "15" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=15"
End If

If ComboBox4.Text = "16" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=16"
End If

If ComboBox4.Text = "17" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=17"
End If

If ComboBox4.Text = "18" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=18"
End If

If ComboBox4.Text = "19" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=19"
End If

If ComboBox4.Text = "20" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=20"
End If

If ComboBox4.Text = "21" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=21"
End If

If ComboBox4.Text = "22" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=22"
End If

If ComboBox4.Text = "23" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=23"
End If

If ComboBox4.Text = "24" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=24"
End If

If ComboBox4.Text = "25" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=25"
End If

If ComboBox4.Text = "26" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=26"
End If

If ComboBox4.Text = "27" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=27"
End If

If ComboBox4.Text = "28" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=28"
End If

If ComboBox4.Text = "29" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=29"
End If

If ComboBox4.Text = "30" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=30"
End If

If ComboBox4.Text = "31" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=31"
End If

If ComboBox4.Text = "32" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=32"
End If

If ComboBox4.Text = "33" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=33"
End If

If ComboBox4.Text = "34" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=34"
End If

If ComboBox4.Text = "35" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=35"
End If

If ComboBox4.Text = "36" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=36"
End If

If ComboBox4.Text = "37" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=37"
End If

If ComboBox4.Text = "38" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=38"
End If

If ComboBox4.Text = "39" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=39"
End If

If ComboBox4.Text = "40" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=40"
End If

If ComboBox4.Text = "41" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=41"
End If

If ComboBox4.Text = "42" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=42"
End If

If ComboBox4.Text = "43" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=43"
End If

If ComboBox4.Text = "44" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=44"
End If

If ComboBox4.Text = "45" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=45"
End If

If ComboBox4.Text = "46" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=46"
End If

If ComboBox4.Text = "47" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=47"
End If

If ComboBox4.Text = "48" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=48"
End If

If ComboBox4.Text = "49" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=49"
End If

If ComboBox4.Text = "50" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=50"
End If

Dim Cell As Range
Dim Unique As New Collection
Dim Valeur As Range
Dim i As Integer
i = Range("B65536").End(xlUp).Row

On Error Resume Next
For Each Cell In Range("B2:B" & i)
If Not Cell.EntireRow.Hidden Then Unique.Add Cell, CStr(Cell)
Next Cell
On Error GoTo 0
For Each Valeur In Unique
Me.ListBox1.AddItem Valeur
Next Valeur


End Sub
 

skoobi

XLDnaute Barbatruc
Re : Gestion de filtre suivant valeur Combobox

Bonsoir,

effectivement, il y a plus court!!!, voici:

Code:
Private Sub ComboBox4_Change()

ListBox1.Clear
Dim Cell As Range
Dim Unique As New Collection
Dim Valeur As Range
Dim i As Integer

If ComboBox4.Text = "" Then
ListBox1.Clear
Selection.AutoFilter Field:=5
End If

For v = 7 To 50
    If ComboBox4.Text = CStr(v) Then
    ListBox1.Clear
    Selection.AutoFilter Field:=5
    Selection.AutoFilter Field:=5, Criteria1:="<=" & CStr(v)
    Exit For
    End If
Next v

i = Range("B65536").End(xlUp).Row

On Error Resume Next
For Each Cell In Range("B2:B" & i)
If Not Cell.EntireRow.Hidden Then Unique.Add Cell, CStr(Cell)
Next Cell
On Error GoTo 0
For Each Valeur In Unique
Me.ListBox1.AddItem Valeur
Next Valeur


End Sub

Remarque: les variables se déclarent en début de code
 

JNP

XLDnaute Barbatruc
Re : Gestion de filtre suivant valeur Combobox

Bonjour Minus ,
J'essayerais :

Private Sub ComboBox4_Change()
ListBox1.Clear
If ComboBox4.Text = "" Then
Selection.AutoFilter Field:=5
End If
If CInt(ComboBox4.Text) >= 7 and CInt(ComboBox4.Text) <= 50 Then
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=5, Criteria1:="<=" & Combo4.Text
End If

Dim Cell As Range
Dim Unique As New Collection
Dim Valeur As Range
Dim i As Integer
i = Range("B65536").End(xlUp).Row
On Error Resume Next
For Each Cell In Range("B2:B" & i)
If Not Cell.EntireRow.Hidden Then Unique.Add Cell, CStr(Cell)
Next Cell
On Error GoTo 0
For Each Valeur In Unique
Me.ListBox1.AddItem Valeur
Next Valeur
End Sub


A te lire
 

Minus

XLDnaute Nouveau
Re : Gestion de filtre suivant valeur Combobox

hehe merci à vous deux ! C'est fou comme on se sent petit face à des formules aussi radicalement simplifiée

Skoobi, rien à redire ! Ca fonctionne.

JNP, 2 soucis :

1 - (binaire...) :

Selection.AutoFilter Field:=5, Criteria1:="<=" & Combobox4.Text

2 - plus dérangeant :

Erreur de codage lorsque je selectionne l'option "".
Renvoie sur la ligne :
If CInt(ComboBox4.Text) >= 7 and CInt(ComboBox4.Text) <= 50 Then

Il n'apprécie pas le fait de revenir sur la variable vide.
 
Dernière édition:

Discussions similaires

Réponses
2
Affichages
449
Réponses
55
Affichages
4 K
  • Question Question
Microsoft 365 Transfert de données
Réponses
7
Affichages
698
Réponses
1
Affichages
402
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…