Protection / deprotection VBA
Bonjour tout le monde,
Mon problème se découle en 2 axes :
1) Sur ce fichier, je l'ai protégé évitant toute modification, cependant pour l'utilisation de deux filtres, j'ai intégré la dé-protection de la feuille le temps d'une macro
Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Qui fait quoi").Unprotect Password:="PAGQFQ"
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
'Tri prénom
If [A3] <> "" Then
Set KeyCells = Range("A3")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
ThisWorkbook.Sheets("Qui fait quoi").Range("A6").AutoFilter Field:=1
ThisWorkbook.Sheets("Qui fait quoi").Range("A6").AutoFilter Field:=6
If KeyCells.Value <> "" Then
[F3] = ""
ThisWorkbook.Sheets("Qui fait quoi").Range("A5").AutoFilter Field:=1, Criteria1:="=*" & KeyCells.Value & "*"
'ThisWorkbook.Sheets("Qui fait quoi").Range("A6").AutoFilter Field:=6, Criteria1:="=*" & KeyCells.Value & "*"
End If
End If
End If
'Tri par métier
If [F3] <> "" Then
Set KeyCells = Range("F3")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
ThisWorkbook.Sheets("Qui fait quoi").Range("A6").AutoFilter Field:=1
ThisWorkbook.Sheets("Qui fait quoi").Range("A6").AutoFilter Field:=6
If KeyCells.Value <> "" Then
[A3] = ""
'ThisWorkbook.Sheets("Qui fait quoi").Range("A5").AutoFilter Field:=1, Criteria1:="=*" & KeyCells.Value & "*"
ThisWorkbook.Sheets("Qui fait quoi").Range("A6").AutoFilter Field:=6, Criteria1:="=*" & KeyCells.Value & "*"
End If
End If
End If
'On réinitialise les filtres
If [A3] = "" And [F3] = "" Then
ThisWorkbook.Sheets("Qui fait quoi").Range("A6").AutoFilter Field:=1
ThisWorkbook.Sheets("Qui fait quoi").Range("A6").AutoFilter Field:=6
End If
'Sheets("Qui fait quoi").Protect Password:="PAGQFQ" '------ICI------
End SubLorsque j'enlève l'apostrophe ma macro ne veut plus fonctionner, je ne comprend ainsi pas pourquoi elle ne veut plus fonctionner
2) Lorsque j'enlève de nouveau l'apostrophe, si je modifie / ajoute / supprime une ligne dans mon tableau dans l'onglet "Qui fait quoi" (A6:F50000) cela me réactive la protection à chaque fois.
Auriez-vous des informations concernant ces deux problèmes.
Je vous remercie d'avance !
QL
Bonjour
Remplacez tout le code par celui ci
Dim stpevt As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If stpevt = True Then Exit Sub
Unprotect Password:="PAGQFQ"
If Not Intersect(Target, Range("A3")) Is Nothing Then
If Target.Value <> "" Then
Unprotect Password:="PAGQFQ"
Range("A6").AutoFilter Field:=1
Range("A6").AutoFilter Field:=6
If Target.Value <> "" Then
stpevt = True
Range("F3") = ""
Range("A5").AutoFilter Field:=1, Criteria1:="=*" & Target.Value & "*"
'ThisWorkbook.Sheets("Qui fait quoi").Range("A6").AutoFilter Field:=6, Criteria1:="=*" & KeyCells.Value & "*"
End If
End If
stpevt = False
End If
'Tri par métier
If Range("F3") <> "" Then
If Not Application.Intersect(Target, Range("F3")) Is Nothing Then
Range("A6").AutoFilter Field:=1
Range("A6").AutoFilter Field:=6
If Target.Value <> "" Then
stpevt = True
Range("A3") = ""
'ThisWorkbook.Sheets("Qui fait quoi").Range("A5").AutoFilter Field:=1, Criteria1:="=*" & Target.Value & "*"
Range("A6").AutoFilter Field:=6, Criteria1:="=*" & Target.Value & "*"
End If
stpevt = False
End If
End If
'On réinitialise les filtres
If Range("A3") = "" And Range("F3") = "" Then
Range("A6").AutoFilter Field:=1
Range("A6").AutoFilter Field:=6
End If
Protect Password:="PAGQFQ"
End SubAttention que la déclaration DIM soit bien positionnée en première ligne ! (donc tout au dessus)
Cordialement
Bonjour,
tu peux aussi protéger ta feuille par macro avec le paramètre UserIterfaceOnly.
Seul l'utilisateur est affecté, tes macros n'ont plus besoin de déprotéger la feuille.
Private Sub Workbook_Open()
With Worksheets("Qui fait quoi")
.EnableAutoFilter = True
.EnableOutlining = True
.Protect Contents:=True, Password:="PAGQFQ", UserInterfaceOnly:=True
End With
End Suberic