Option Explicit
Dim heslo As String
Sub NastavitHeslo()
heslo = "tvojeHeslo"
End Sub
Sub ZamknutZosit()
Call NastavitHeslo
Dim ws As Worksheet
' Zamknutie konkrétnych hárkov
Set ws = ThisWorkbook.Sheets("Hárok1")
ws.Unprotect Password:=heslo
ws.Cells.Locked = True
ws.Range("H5,H7,E13:E64,E76:G128,G13:G64,G76:G128,D132,D134,D136,D138").Locked = False
ws.Protect Password:=heslo, AllowUsingPivotTables:=True, AllowSorting:=True, AllowFiltering:=True, AllowFormattingCells:=True
ws.EnableSelection = xlUnlockedCells
Set ws = ThisWorkbook.Sheets("Hárok2")
ws.Unprotect Password:=heslo
ws.Cells.Locked = True
ws.Range("D2").Locked = False
ws.Protect Password:=heslo, AllowUsingPivotTables:=True, AllowSorting:=True, AllowFiltering:=True, AllowFormattingCells:=True
ws.EnableSelection = xlUnlockedCells
Set ws = ThisWorkbook.Sheets("Hárok5")
ws.Unprotect Password:=heslo
ws.Cells.Locked = True
ws.Range("E1,F1,E53:E56,H53:H56").Locked = False
ws.Protect Password:=heslo, AllowUsingPivotTables:=True, AllowSorting:=True, AllowFiltering:=True, AllowFormattingCells:=True
ws.EnableSelection = xlUnlockedCells
' Zamknutie celého zošita
ThisWorkbook.Protect Password:=heslo, Structure:=True, Windows:=False
MsgBox "Zošit a hárky boli úspešne zamknuté."
End Sub
Sub OdomknutZosit()
Call NastavitHeslo
Dim ws As Worksheet
' Odomknutie konkrétnych hárkov
Set ws = ThisWorkbook.Sheets("Hárok1")
ws.Unprotect Password:=heslo
ws.Range("H5,H7,E13:E64,E76:G128,G13:G64,G76:G128,D132,D134,D136,D138").Locked = True
Set ws = ThisWorkbook.Sheets("Hárok2")
ws.Unprotect Password:=heslo
ws.Range("D2").Locked = True
Set ws = ThisWorkbook.Sheets("Hárok5")
ws.Unprotect Password:=heslo
ws.Range("E1,F1,E53:E56,H53:H56").Locked = True
' Odomknutie celého zošita
ThisWorkbook.Unprotect Password:=heslo
MsgBox "Zošit a hárky boli úspešne odomknuté."
End Sub
citovat