Re,
Qu'entends-tu par ça ne marche pas, bonne question non ?
Ca plante ? Ca ne fait pas le job ? Il faudrait plus d'explications non ?
Il nous faudrais aussi le nom de toutes les feuilles du classeur pour voir si la façon de faire est bonne.
je viens de me rendre compte de plusieurs bourdes de ma part, mais bon en lisant le code tu aurais du trouver.
- PtrotectSheets et mal orthographié, mais cela ne gène en rien le fonctionnement
- FirstSheetNumber doit-être définit sur la première feuille à protéger
- .Item("Feuil" & CStr(Counter)).Protect Password:="cqcondi", ..... Ajoute le Cstr(), mais je pense qu'Excel arrive à le faire de lui même
Donc au final on a :
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ProtectSheets 6
End Sub
' // Il vaut mieux mettre ce code dans un module distinct
Public Sub ProtectSheets(ByVal FirstSheetNumber As Long)
Dim Counter As Long
With ThisWorkbook.Worksheets
Counter = FirstSheetNumber
Do While Counter <= .Count
If IsWorksheetExists("Feuil" & CStr(Counter)) Then
.Item("Feuil" & CStr(Counter)).Protect _
Password:="cqcondi", DrawingObjects:=True, _
Contents:=True, Scenarios:=True, UserInterfaceOnly:=True, _
AllowFormattingCells:=False, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=False, _
AllowInsertingRows:=False, AllowInsertingHyperlinks:=False, _
AllowDeletingColumns:=False, AllowDeletingRows:=False, AllowSorting:=False, _
AllowFiltering:=False, AllowUsingPivotTables:=False
End If
Counter = Counter + 1
Loop
End With
End Sub
Public Function IsWorksheetExists(ByVal SheetName As String) As Boolean
Dim localWorksheet As Worksheet
For Each localWorksheet In ThisWorkbook.Worksheets
If StrComp(localWorksheet.Name, SheetName, vbTextCompare) = 0 Then
IsWorksheetExists = True
Exit For
End If
Next
End Function