-
Notifications
You must be signed in to change notification settings - Fork 4
/
lockAllSheets.bas
75 lines (58 loc) · 3.03 KB
/
lockAllSheets.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''''''''''''''''''''''''''''''''''''''''''''''
' Lock or Unlock All Worksheets '
'''''''''''''''''''''''''''''''''''''''''''''''
'recieves an input of optional locked (ex. True) and an optional input of singleSheet (ex. ThisWorkbook.Sheets("Sheet 1"))
'locks or unlocks sheets based on locked input
'this sub will only lock the user interface features of the worksheets so that they can continue to be manipulated by
'VBA without the need to unlock
'this sub will allow the insertion of hyperlinks in unlocked cells
'this sub will allow the selection of unlocked cells
'''locked
'if locked is not supplied the sheets will be locked by default
'''singleSheet
'if singleSheet is not supplied all sheets on the workbook will be locked
Sub lockAllSheets(Optional locked As Boolean = True, Optional singleSheet As Worksheet)
Dim wks As Worksheet
Dim password as String: password = ""
'if singleSheet was supplied
If Not singleSheet Is Nothing Then
'if unlock was requested then ulock the single sheet else lock the single sheet
If Not locked Then
singleSheet.Unprotect Password:=password
Else
With singleSheet
.Protect Password:=password, UserInterfaceOnly:=True, AllowInsertingHyperlinks:=True
.EnableSelection = xlUnlockedCells
End With
End If
'if singleSheet was NOT supplied
Else
'if unlock was requested
If Not locked Then
'loop through each sheet in the workbook
For Each wks In ActiveWorkbook.Worksheets
'only unlock sheets that are locked
If wks.ProtectContents = True Then
'unlock sheet
With wks
.Unprotect Password:=password
.EnableSelection = xlNoRestrictions
End With
End If
Next wks
'if lock was requested
Else
'loop through each sheet in the workbook
For Each wks In ActiveWorkbook.Worksheets
'only lock sheets that are unlocked
If wks.ProtectContents = False Then
'lock sheet
With wks
.Protect Password:=password, UserInterfaceOnly:=True, AllowInsertingHyperlinks:=True
.EnableSelection = xlUnlockedCells
End With
End If
Next wks
End If
End If
End Sub