Workbook Password Protection
Add password protection to workbooks and worksheets with various security levels
130 views
Perfect For:
- Data security
- Access control
- Confidential information
VBA Code
Sub ProtectWorkbook()
Dim password As String
Dim protectionType As String
password = InputBox("Enter password for protection:")
If password = "" Then Exit Sub
protectionType = InputBox("Protection type: WORKBOOK, SHEET, or BOTH:", "Protection Type")
Select Case UCase(protectionType)
Case "WORKBOOK"
ThisWorkbook.Protect Password:=password, Structure:=True, Windows:=True
MsgBox "Workbook structure protected!"
Case "SHEET"
ActiveSheet.Protect Password:=password, _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True, _
AllowSorting:=True, _
AllowFiltering:=True, _
AllowFormattingCells:=False
MsgBox "Current sheet protected with formatting restrictions!"
Case "BOTH"
' Protect workbook structure
ThisWorkbook.Protect Password:=password, Structure:=True, Windows:=True
' Protect all sheets
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Protect Password:=password, _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True, _
AllowSorting:=True, _
AllowFiltering:=True
Next ws
MsgBox "Workbook and all sheets protected!"
End Select
End Sub
Related Topics
password
protection
security
access
Need Custom VBA Solutions?
Our AI-powered VBA generator can create custom code tailored to your specific requirements in seconds.
Generate Custom VBA Code