Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Filtre automatique vba

2 réponses
Avatar
JP
Bonjour,

Dans une macro d'impression, j'ai un soucis avec les filtres.
A la premi=E8re utilisation, quand aucun filtre n'est implant=E9, la macro =
fonctionne.
Par la suite, elle dysfonctionne. Le filtre devrait se positionner sur une =
colonne. Elle peut diff=E9rer d'une fois =E0 l'autre.
Dans mon code (rang) indique la colonne o=F9 devrait se positionner le filt=
re.

voici le code car je ne sais pas o=F9 se produit l'erreur.

Merci d'avance pour le conseil qui va me d=E9panner.

JP

Sub Imprime_SGF()
=20

With Worksheets("Strat Globale")
If .FilterMode =3D True Then .ShowAllData
End With

Application.ScreenUpdating =3D False

If UserForm1.ComboBox1 =3D "Toute" Then

With Sheets("Strat Globale")
.Activate
' D=E9finition de la plage =E0 imprimer
DerLigne =3D .Range("T" & Rows.Count).End(xlUp).Row
Application.EnableEvents =3D True
.Cells(DerLigne, 5).Select
Application.EnableEvents =3D False
Unload UserForm1
.PrintPreview
End With


Else
=20
With Sheets("Strat Globale")
.Activate

rang =3D .Range("H5:N5").Find(what:=3DUserForm1.ComboBox1.Value).Co=
lumn
Range(Cells(5, rang), Cells(10, rang)).Select
Selection.AutoFilter Field:=3D1, Criteria1:=3D"X"
Application.EnableEvents =3D True
' D=E9finition de la plage =E0 imprimer
DerLigne =3D .Range("T" & Rows.Count).End(xlUp).Row
.PageSetup.PrintArea =3D ("$E$2:$X" & DerLigne)
.Cells(DerLigne, 5).Select
Application.EnableEvents =3D False
End With
=20
Application.PrintCommunication =3D True
ActiveWindow.SelectedSheets.PrintOut Copies:=3D1, Collate:=3DTrue, Igno=
rePrintAreas:=3DFalse

End If
Application.ScreenUpdating =3D True
=20
Sheets("Accueil").Select
Range("B27").Select

End Sub

2 réponses

Avatar
MichD
Bonjour,

Essaie comme ceci :

'-------------------------------------------------------------
Sub Imprime_SGF()
Dim Trouve As Range, Rang As Long
Dim Sh As Worksheet, DerLigne As Long

Application.ScreenUpdating = False
Application.EnableEvents = False

Set Sh = Worksheets("Strat Globale")
Sh.Select

With Sh
If .FilterMode = True Then .ShowAllData
End With

If UserForm1.ComboBox1 = "Toute" Then
With Sh
.Activate
' Définition de la plage à imprimer
DerLigne = .Range("T" & .Rows.Count).End(xlUp).Row
.Cells(DerLigne, 5).Select
UserForm1.Hide
.PrintPreview
UserForm1.Show 0
End With
Else
If UserForm1.ComboBox1.Value <> "" Then
With Sh
With .Range("H5:N5")
Set Trouve = .Find(what:=UserForm1.ComboBox1.Value, _
LookIn:=xlValues, lookAt:=xlWhole)
If Not Trouve Is Nothing Then
Rang = Trouve.Column
Else
Application.EnableEvents = True
MsgBox "Valeur inexistante dans la plage."
Exit Sub
End If
End With
With .Range(Cells(5, Rang), Cells(10, Rang))
.AutoFilter Field:=1, Criteria1:="X"
DerLigne = Sh.Range("T" & Rows.Count).End(xlUp).Row
If DerLigne < 5 Then DerLigne = 5
End With

.PageSetup.PrintArea = .Range("$E$2:$X" & DerLigne).Address
.Cells(DerLigne, 5).Select
UserForm1.Hide
.PrintPreview 'tu changes pour .PrintOut après test.
UserForm1.Show 0
.Range(Cells(5, Rang), Cells(10, Rang)).AutoFilter
End With
End If
End If

With Sheets("Accueil")
.Select
.Range("B27").Select
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
'-------------------------------------------------------------
Avatar
JP
Bonsoir Denis,

Très bien, tout fonctionne.
J'ai choisi de mettre PrintPreview car il peut y avoir beaucoup de pages da ns l'hypothèse où le document n'est pas filtré.
Je verrai bien à l'usage si j'imprime directement ou si l'aperçu reste une bonne solution.

En tout cas merci beaucoup pour la modif.

JP