idate
-
27 août 2013 à 12:06
eriiic
Messages postés24571Date d'inscriptionmardi 11 septembre 2007StatutContributeurDernière intervention19 mai 2024
-
28 août 2013 à 23:58
Bonjour,
Je confectionne actuellement un petit programme en VBA pour dégager une sélection de produit de ma base adapté au besoin d'un internaute. Je récupère le besoin de l'internaute grâce à un formulaire contenant des TextBox et des ComboBox.
Je stocke ces données dans des variables et m'en sers pour filtrer avec la fonction Autofilter.
Cependant, j'ai deux soucis:
- Dans mon programme je fais un autofilter sur une feuille, puis un autre autofilter sur une autre feuille. Le premier marche niquel, à savoir il filtre en colonne et m'affiche les enregistrements correspondants. Le second place les filtres sur les colonnes mais ne m'affiche pas les enregistrements correspondants. Pour vérifier, je suis aller dans un des filtres (filtres perso) puis j'ai validé, et là, il m'affiche les enregistrements. Du coup, c'est assez embêtant.
- Mon formulaire contient des champs optionnels. Quand ils sont non vides, ces champs me permettent de préciser ma recherche à l'aide de nouveaux filtres. Cependant, quand je ne les remplis pas, mon programme filtre quand même la colonne par 0 ou Valeur nulle. J'ai mis une condition pour éviter cela mais rien à faire...
Voici mon code:
Private Sub CommandButton1_Click()
'Variables
Dim type_f As String
Dim largeur_f As String
Dim forme_f As String
Dim type_porte_f As String
Dim hauteur_porte_f As Double
Dim materiau_f As String
Dim nb_battant_f As Integer
Dim pilier_f As Double
Dim coteC_f As Double
Dim coteE_f As Double
Dim ouv_f As Double
Dim tension_f As Integer
Dim poids_max_min_k As Double
Dim poids_max_max_k As Double
Dim traction_max_max_k As Double
Dim traction_max_min_k As Double
Dim largeur_max_min_k As Double
Dim largeur_max_max_k As Double
Dim hauteur_max_min_k As Double
Dim hauteur_max_max_k As Double
Dim tmp As Range
Dim result As Range
'Stockage des infos clients
If ComboBox1.Value = "PORTAIL BATTANT" Then
type_f = "BATTANT"
largeur_f = ComboBox2.Value
forme_f = ComboBox3.Value
materiau_f = ComboBox4.Value
If Not ComboBox5.Value = "" Then nb_battant_f = ComboBox5.Value
If Not ComboBox6.Value = "" Then tension_f = ComboBox6.Value
If Not TextBox1.Value = "" Then pilier_f = TextBox1.Value
If Not TextBox2.Value = "" Then coteC_f = TextBox2.Value
If Not TextBox3.Value = "" Then coteE_f = TextBox3.Value
ElseIf ComboBox1.Value = "PORTAIL COULISSANT" Then
With Worksheets("tmp")
Worksheets("tmp").Activate
poids_max_min_k = .Range("L2")
poids_max_max_k = .Range("M2")
traction_max_min_k = .Range("N2")
traction_max_max_k = .Range("O2")
largeur_max_min_k = .Range("P2")
largeur_max_max_k = .Range("Q2")
hauteur_max_min_k = .Range("R2")
hauteur_max_max_k = .Range("S2")
End With
If ComboBox1.Value = "PORTAIL BATTANT" Then
'Traitement des résultats
With Worksheets("BASE")
.Activate
.AutoFilterMode = False
If .FilterMode = True Then .ShowAllData
.Range("A1").AutoFilter Field:=9, Criteria1:="PORTAIL BATTANT"
.Range("A1").AutoFilter Field:=20, Criteria1:=">=" & largeur_max_min_k, Operator:=xlAnd, Criteria2:="<" & largeur_max_max_k
.Range("A1").AutoFilter Field:=21, Criteria1:=">=" & poids_max_min_k, Operator:=xlAnd, Criteria2:="<" & poids_max_max_k
If Not IsNull(nb_battant_f) Then .Range("A1").AutoFilter Field:=11, Criteria1:=nb_battant_f
If Not IsNull(tension_f) Then .Range("A1").AutoFilter Field:=25, Criteria1:=tension_f
If Not IsNull(pilier_f) Then .Range("A1").AutoFilter Field:=12, Criteria1:="<=" & pilier_f
If Not IsNull(coteC_f) Then .Range("A1").AutoFilter Field:=13, Criteria1:="<=" & coteC_max
If Not IsNull(coteE_f) Then .Range("A1").AutoFilter Field:=14, Criteria1:="<=" & coteE_max
Set result = ActiveCell.CurrentRegion.SpecialCells(xlCellTypeVisible)
result.Select
result.Copy Feuil9.Range("A1")
End With
ElseIf ComboBox1.Value = "PORTAIL COULISSANT" Then
With Worksheets("BASE")
.Activate
.AutoFilterMode = False
If .FilterMode = True Then .ShowAllData