Fonction Autofilter problématique

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

type_f = "COULISSANT"
largeur_f = ComboBox2.Value
forme_f = ComboBox3.Value
materiau_f = ComboBox4.Value
tension_f = OptionButton3.Value

ElseIf ComboBox1.Value = "PORTE GARAGE" Then

type_f = "GARAGE"
type_porte_f = ComboBox2.Value
hauteur_porte_f = ComboBox3.Value
tension_f = OptionButton3.Value

End If

Worksheets("Relations").Activate

If ComboBox1.Value = "PORTAIL BATTANT" Then
'Traitement du besoin
With Worksheets("Relations")
    If .FilterMode = True Then .ShowAllData

    .Range("A1").AutoFilter Field:=2, Criteria1:=type_f
    .Range("A1").AutoFilter Field:=6, Criteria1:=largeur_f
    .Range("A1").AutoFilter Field:=7, Criteria1:=forme_f
    .Range("A1").AutoFilter Field:=8, Criteria1:=materiau_f
     Set tmp = ActiveCell.CurrentRegion.SpecialCells(xlCellTypeVisible)
     tmp.Select
     tmp.Copy Feuil7.Range("A1")

End With

ElseIf ComboBox1.Value = "PORTAIL COULISSANT" Then
With Worksheets("Relations")
    If .FilterMode = True Then .ShowAllData

    .Range("A1").AutoFilter Field:=2, Criteria1:=type_f
    .Range("A1").AutoFilter Field:=6, Criteria1:=largeur_f
    .Range("A1").AutoFilter Field:=7, Criteria1:=forme_f
    .Range("A1").AutoFilter Field:=8, Criteria1:=materiau_f
     Set tmp = ActiveCell.CurrentRegion.SpecialCells(xlCellTypeVisible)
     tmp.Select
    tmp.Copy Feuil7.Range("A1")

End With

ElseIf ComboBox1.Value = "PORTE GARAGE" Then
With Worksheets("Relations")
    If .FilterMode = True Then .ShowAllData

    .Range("A1").AutoFilter Field:=2, Criteria1:=type_f
    .Range("A1").AutoFilter Field:=3, Criteria1:=type_porte_f
    .Range("A1").AutoFilter Field:=6, Criteria1:=hauteur_porte_f
     Set tmp = ActiveCell.CurrentRegion.SpecialCells(xlCellTypeVisible)
     tmp.Select
    tmp.Copy Feuil7.Range("A1")

End With

End If

Worksheets("Relations").AutoFilterMode = False

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

    .Range("A1").AutoFilter Field:=9, Criteria1:="PORTAIL COULISSANT"
    .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
     Set result = ActiveCell.CurrentRegion.SpecialCells(xlCellTypeVisible)
     result.Select
     result.Copy Feuil9.Range("A1")

End With

ElseIf ComboBox1.Value = "PORTE GARAGE" Then
With Worksheets("BASE")
    .Activate
    .AutoFilterMode = False
    If .FilterMode = True Then .ShowAllData

    .Range("A1").AutoFilter Field:=9, Criteria1:="PORTE GARAGE"
    .Range("A1").AutoFilter Field:=15, Criteria1:=">=" & hauteur_max_min_k, Operator:=xlAnd, Criteria2:="<" & hauteur_max_max_k
    .Range("A1").AutoFilter Field:=22, Criteria1:=">=" & traction_max_min_k, Operator:=xlAnd, Criteria2:="<" & traction_max_max_k
     Set result = ActiveCell.CurrentRegion.SpecialCells(xlCellTypeVisible)

     result.Select
     result.Copy Feuil9.Range("A1")

End With

End If
End Sub

Pouvez-vous m'aider à déceler ce qui pourrait causer ces dysfonctionnements?

Merci d'avance

Bonjour

Regardes ce post https://forum.excel-pratique.com/excel/filtre-nulerique-textuels-t42359.html

il y a peut-être surement la solution que tu cherches

Si des difficultés il faut joindre ton fichier

Merci de votre réponse.

Le topic ciblé ne m'a pas apporté de solution.

Je vous joins mon fichier.

Je ne comprend pas pourquoi sur une feuille ("Relations") les Autofilter marchent très bien mais sur la feuille "BASE" non...

Bonjour

Je confirme la solution était dans le lien

A vérifier

J'ai modifié peut-être beaucoup car je ne connais pas assez ton programme

Il faut utiliser cette méthode lorsque tes variables peuvent contenir un nombre décimal, sinon cela ne gène pas si tu le fais pour un nombre entier

Désolé pourtant je pensais pas que le Replace serait la solution !

Encore merci, vous êtes un chef !

Rechercher des sujets similaires à "fonction autofilter problematique"