XL 2019 Plusieurs Worksheet_Change sur la même feuille

Rabeto

XLDnaute Occasionnel
Bonjour à tous,

Quelqu'un a une solution svp

J'aimerai fusionner ces 2 macros qui font des filtres automatique selon la valeur d'une cellule sur la même feuille svp
Est ce possible également d'enlever le filtre avant le lancement de chaque macro ?

-------------------------------------------------------
Private Sub Worksheet_Change(ByVal T As Range)
If Not Intersect(T, [A1]) Is Nothing Then
Range("A15:A" & Cells(Rows.Count, "A").End(xlUp).Row).AutoFilter 1, [A1]
End If
End Sub
-------------------------------------------------------
Private Sub Worksheet_Change(ByVal X As Range)
If Not Intersect(X, [B1]) Is Nothing Then
Range("B15:B" & Cells(Rows.Count, "B").End(xlUp).Row).AutoFilter 1, [B1]
End If
End Sub
-------------------------------------------------------

PS : j'ai trouvé des sujets similaires sur le forum mais je n'arrive pas à adapter mon code avec les solutions proposées.
 

Pièces jointes

  • Worksheet_Change.xlsx
    9.2 KB · Affichages: 3
Solution
Bonjour à tous,
VB:
Private Sub Worksheet_Change(ByVal T As Range)
With Range("A15:B" & Cells.SpecialCells(xlCellTypeLastCell).Row)
    If .Row < 15 Then Exit Sub
    If Not Intersect(T, [A1:B1]) Is Nothing Then .AutoFilter: .AutoFilter 'ôte le filtre
    If Not Intersect(T, [A1]) Is Nothing Then .AutoFilter 1, [A1]
    If Not Intersect(T, [B1]) Is Nothing Then .AutoFilter 2, [B1]
End With
End Sub
A+

fanfan38

XLDnaute Barbatruc
Bonjour
Est ce seulement A1 et B1 qui change ou une cellule de la colonne...
Ci joint ma solution pour A1 ou B1
VB:
Private Sub Worksheet_Change(ByVal T As Range)
  T.AutoFilter
  If T.Address = "$A$1" Then
    Range("A15:A" & Cells(Rows.Count, "A").End(xlUp).Row).AutoFilter 1, [A1]
  ElseIf T.Address = "$B$1" Then
    Range("B15:B" & Cells(Rows.Count, "B").End(xlUp).Row).AutoFilter 1, [B1]
  End If
End Sub
A+ François
 

patricktoulon

XLDnaute Barbatruc
re
effectivement @fanfan38 a raison ,si c'est simplement A1 ou B1 on peut simplifier
VB:
Private Sub Worksheet_Change(ByVal T As Range)
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False

    Select Case T.Address(0, 0)

    Case "A1": Range("A15:A" & Cells(Rows.Count, "A").End(xlUp).Row).AutoFilter 1, [A1]

    Case "B1": Range("B15:B" & Cells(Rows.Count, "B").End(xlUp).Row).AutoFilter 1, [B1]

    End Select

End Sub
 

job75

XLDnaute Barbatruc
Bonjour à tous,
VB:
Private Sub Worksheet_Change(ByVal T As Range)
With Range("A15:B" & Cells.SpecialCells(xlCellTypeLastCell).Row)
    If .Row < 15 Then Exit Sub
    If Not Intersect(T, [A1:B1]) Is Nothing Then .AutoFilter: .AutoFilter 'ôte le filtre
    If Not Intersect(T, [A1]) Is Nothing Then .AutoFilter 1, [A1]
    If Not Intersect(T, [B1]) Is Nothing Then .AutoFilter 2, [B1]
End With
End Sub
A+
 

patricktoulon

XLDnaute Barbatruc
Bonjour
compile tout dans un seul event dans un select case true
VB:
Private Sub Worksheet_Change(ByVal T As Range)
     If ActiveSheet.AutoFilterMode Then
       ActiveSheet.AutoFilterMode = False
     End If
    Select Case True
    Case Not Intersect(T, [A1]) Is Nothing
        Range("A15:A" & Cells(Rows.Count, "A").End(xlUp).Row).AutoFilter 1, [A1]

    Case Not Intersect(X, [B1]) Is Nothing
        Range("B15:B" & Cells(Rows.Count, "B").End(xlUp).Row).AutoFilter 1, [B1]

        'case blablabla
        'etc..etc..
    End Select

End Sub
 

Statistiques des forums

Discussions
312 367
Messages
2 087 644
Membres
103 627
dernier inscrit
nabil