Protect-Unprotect all sheets

Închis
zvonacfirst
Mesaje: 105
Membru din: Mie Feb 19, 2014 10:41 pm

Protect-Unprotect all sheets

Mesaj de zvonacfirst » Mar Oct 10, 2017 6:17 pm

Buna.

Codurile VBA de mai jos as vrea sa le utilizez pentru Protect/Unprotect toate sheeturile unui workbook.
Nu stiu unde gresesc. Codurile functioneaza doar in sheetul activ.
Rog ajutor. Multumesc.

Cod: Selectaţi tot

Sub ProtectAllSheets()

Dim sh As Worksheet
On Error Resume Next
For Each sh In ActiveWorkbook.Sheets
    ActiveSheet.Unprotect ""
    Application.CutCopyMode = False
    ActiveSheet.Protect "", DrawingObjects:=True, Contents:=True, Scenarios:=True
    ActiveSheet.EnableSelection = xlNoSelection
Next sh
End Sub

Cod: Selectaţi tot

Sub UnprotectAllSheets()

Dim sh As Worksheet
On Error Resume Next
For Each sh In ActiveWorkbook.Sheets
    ActiveSheet.Unprotect ""
    Application.CutCopyMode = True
    ActiveSheet.Protect "", DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFormattingCells:=True, AllowFormattingRows:=True
    ActiveSheet.EnableSelection = xlUnlockedCells
Next sh
End Sub

Nills
Mesaje: 211
Membru din: Sâm Ian 23, 2016 11:24 am

Re: Protect-Unprotect all sheets

Mesaj de Nills » Mar Oct 10, 2017 10:27 pm

Sal,

Cele 2 coduri fac acelasi lucru, protejeaza foile din fisier.
Nu stiu unde gresesc. Codurile functioneaza doar in sheetul activ.
pai folosesti ActiveSheet e normal sa functioneze in foaia active.
For Each sh In ActiveWorkbook.Sheets
ActiveSheet.Unprotect ""
....
....
si ar trebui sa folosesti acel sh din acel ciclu for:

Cod: Selectaţi tot

For Each sh In ActiveWorkbook.Sheets
    sh.Unprotect ""
....
....
Folosind codurile prezentate"

Cod: Selectaţi tot

Sub ProtectAllSheets()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
    sh.Protect "", DrawingObjects:=True, Contents:=True, Scenarios:=True 
Next sh
End Sub

Cod: Selectaţi tot

Sub UnprotectAllSheets()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
    sh.Unprotect ""
Next sh
End Sub
I don't care what you think of me! Unless you think I'm awesome – in which case, you're right! Carry on :D

zvonacfirst
Mesaje: 105
Membru din: Mie Feb 19, 2014 10:41 pm

Re: Protect-Unprotect all sheets

Mesaj de zvonacfirst » Mie Oct 11, 2017 7:19 am

Asta era problema, am schimbat ActiveSheet cu sh si merge.

Multumesc mult.

Închis

Înapoi la “Visual Basic for Application (VBA) cu Excel - Intrebari tehnice”