Protection feuille et utilisation macro

Bonjour à tous,

J'ai une petite question, j'ai une feuille avec des macros, lorsque je verrouille la feuille ces dernières ne fonctionne pas, enfin j'ai une erreur 400.

J'ai gratté un petit peu, j'ai trouvé le code ci-dessous, mais d'une part je ne sais s'il es correct et d'autre part je ne sais pas comment intégrer ce code macro de protection dans le code global.

Bien cordialement


Sub MacroavecfeuilleProtect()
    ActiveSheet.Unprotect "lemotdepasse"
'Placez ici vos instructions
    ActiveSheet.Protect "lemotdepasse", True, True, True
End Sub

Les macros de la feuille sont les suivantes :

Option Explicit

Sub Nettoyage1453()
Dim NbLg As Long

  Application.ScreenUpdating = False
  NbLg = Columns("B:H").Find(what:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  Range("B18:H" & NbLg).Value = Range("B18:H" & NbLg).Value
  With Range("I18:I" & NbLg)
    .Formula = "=IF(OR(H18=0,H18=""""),"""",""X"")"
    .Value = .Value
    On Error Resume Next
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    .ClearContents
    On Error GoTo 0   ' Si on efface toutes les lignes Clearcontents provoque une erreur
  End With
  NbLg = Range("B" & Rows.Count).End(xlUp).Row
  With Range("I18:I" & NbLg)
    .Formula = "=LEFT(B18,1)&MID(B18,3,2)*1"
    .Value = .Value
    Range("A18:I" & NbLg).Sort key1:=Range("I18"), order1:=xlAscending, dataoption1:=xlSortNormal, Header:=xlNo
    .ClearContents
  End With

  NbLg = Range("B" & Rows.Count).End(xlUp).Row + 1
  Range("H" & NbLg).Value = Range("H" & NbLg).Value
End Sub

Sub InitialiseFormule1453()
Dim NbLg As Long

  NbLg = Application.Max(18, Columns("B:L").Find(what:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row)
  Range("B18:L" & NbLg).Clear
  If Range("E10") = "" Then
   MsgBox "Veuillez choisir une structure"
   Exit Sub
  End If
  'NbLg = Application.CountIf(Sheets("Liste complète des PERS").Columns("G"), Range("E10")) + 17
  NbLg = Application.CountIf(Sheets("Personnels").Columns("G"), Range("E10")) + 17
  Application.ScreenUpdating = False
  Range("B18").Formula = "=IF(Personnels!A1="""","""",MID(Personnels!A1,2,1)&"" ""&MID(Personnels!A1,3,2)&"" ""&MID(Personnels!A1,5,2)&"" ""&MID(Personnels!A1,7,2)&"" ""&MID(Personnels!A1,9,3)&"" ""&MID(Personnels!A1,12,3)&"" ""&CHAR(124)&"" ""&MID(Personnels!A1,15,2))"
  Range("C18").Formula = "=Personnels!F1"
  Range("D18").Formula = "=IF(INDIRECT(""Planning!""&ADDRESS(1,ROW()-15))="""","""",INDIRECT(""Planning!""&ADDRESS(1,ROW()-15)))"
  Range("E18").Formula = "=Personnels!C1"
  Range("F18").Formula = "=IF(D18="""","""",INDIRECT(""Planning!""&ADDRESS(40,ROW()-15)))"
  Range("G18").Formula = "=IF(D18<>"""",2.64,"""")"
  Range("H18").Formula = "=IF(D18<>"""",F18*G18,"""")"
  Range("B18:H18").AutoFill Destination:=Range("B18:H" & NbLg), Type:=xlFillSeries
  With Range("B18:H" & NbLg)
    .Font.Name = "Verdana"
    .Borders.Weight = xlThin
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
  End With
  Range("G18:H" & NbLg).NumberFormat = "#,##0.00 $"
  LigneTotal NbLg + 1
End Sub

Sub LigneTotal(Ligne As Long)
  Range("H" & Ligne).NumberFormat = "#,##0.00 $"
  Range("H" & Ligne).Formula = "=SUM(H18:H" & Ligne - 1 & ")"
  Range("F" & Ligne).Value = "Total"
  With Range("F" & Ligne).Resize(1, 2)
    .Merge
    .Font.Bold = True
  End With
  With Range("F" & Ligne).Resize(1, 3)
    .Font.Name = "Verdana"
    .Font.Size = 10
    '.Interior.ColorIndex = 3
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .Borders.Weight = xlThin
  End With
  Range("H" & Ligne).Font.Bold = True
End Sub

Cordialement.

Bonjour

Si ton mot de passe est « lemotdepasse »,

Tu places l’instruction qui ote la protection de ta feuille en début de macro :

Sub Nettoyage1453()
Dim NbLg As Long
ActiveSheet.Unprotect "lemotdepasse"

Et tu la remets juste avant qu’elle ne termine ce qu’elle a à faire :

ActiveSheet.Unprotect "lemotdepasse"
End Sub

Bonsoir Gmb,

Merci pour ton intervention, car je ne savais pas comment l'intégrer..

En fait pour fermer j'ai mis ce code :

ActiveSheet.Protect "lemotdepasse"

Ca fonctionne, merci bien et bonne soirée.

Cordialement.

Rechercher des sujets similaires à "protection feuille utilisation macro"