ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Protecting whole workbook (https://www.excelbanter.com/excel-programming/388273-protecting-whole-workbook.html)

Tanya

Protecting whole workbook
 
Hi,
I have a workbook with approx 30 worksheets and have protected each
worksheet with the following macro's

Private Sub Protect_Workbook_Click()
'Protect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = False Then
ws.Protect ("BBHS")
End If
Next
ActiveWorkbook.Protect (["BBHS"])
Application.ScreenUpdating = True


End Sub

Private Sub UnProtect_Workbook_Click()
'Unprotect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = True Then
ws.Unprotect ("BBHS")
End If
Next
ActiveWorkbook.Unprotect (["BBHS"])
Application.ScreenUpdating = True


End Sub

What I am finding is after I run the protection macro is each worksheet
allows you still to click in every cell. My question is, how do get the
macro to protect the worksheets in such a way that you can only click on a
cell that is unprotected?
If anyone could help with this I would be appreciate it greatly.
Regards
Tanya



Mike H

Protecting whole workbook
 
Very minor change to your code will do the trick:-

Private Sub Protect_Workbook_Click()
'Protect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = False Then
ws.Protect ("BBHS")
ActiveSheet.EnableSelection = xlUnlockedCells
End If
Next
ActiveWorkbook.Protect (["BBHS"])
Application.ScreenUpdating = True

Mike


End Sub

"Tanya" wrote:

Hi,
I have a workbook with approx 30 worksheets and have protected each
worksheet with the following macro's

Private Sub Protect_Workbook_Click()
'Protect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = False Then
ws.Protect ("BBHS")
End If
Next
ActiveWorkbook.Protect (["BBHS"])
Application.ScreenUpdating = True


End Sub

Private Sub UnProtect_Workbook_Click()
'Unprotect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = True Then
ws.Unprotect ("BBHS")
End If
Next
ActiveWorkbook.Unprotect (["BBHS"])
Application.ScreenUpdating = True


End Sub

What I am finding is after I run the protection macro is each worksheet
allows you still to click in every cell. My question is, how do get the
macro to protect the worksheets in such a way that you can only click on a
cell that is unprotected?
If anyone could help with this I would be appreciate it greatly.
Regards
Tanya



Norman Jones

Protecting whole workbook
 
Hi Tanya,

See the EnableSelection property in VBA help.

However, this property is not persistant, Therefore, try protecting the
sheets in the Workbook_Open procedu

'<<=============
Private Sub Protect_Workbook_Click()
Dim ws As Worksheet
Const PWORD As String = "Pippo"

For Each ws In Me.Worksheets
With ws
If .ProtectContents = False Then
.EnableSelection = xlUnlockedCells
.Protect password:=PWORD
End If
End With
Next ws

ActiveWorkbook.Protect password:=PWORD
End Sub
'<<=============

This is workbook event code and should be pasted into
the workbook's ThisWorkbook module *not* a standard
module or a sheet module:

Right-click the Excel icon on the worksheet
(or the icon to the left of the File menu if your workbook is
maximised)
Select 'View Code' from the menu and paste the code.
Alt-F11 to return to Excel.


---
Regards,
Norman


"Tanya" wrote in message
...
Hi,
I have a workbook with approx 30 worksheets and have protected each
worksheet with the following macro's

Private Sub Protect_Workbook_Click()
'Protect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = False Then
ws.Protect ("BBHS")
End If
Next
ActiveWorkbook.Protect (["BBHS"])
Application.ScreenUpdating = True


End Sub

Private Sub UnProtect_Workbook_Click()
'Unprotect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = True Then
ws.Unprotect ("BBHS")
End If
Next
ActiveWorkbook.Unprotect (["BBHS"])
Application.ScreenUpdating = True


End Sub

What I am finding is after I run the protection macro is each worksheet
allows you still to click in every cell. My question is, how do get the
macro to protect the worksheets in such a way that you can only click on a
cell that is unprotected?
If anyone could help with this I would be appreciate it greatly.
Regards
Tanya





Norman Jones

Protecting whole workbook
 
Hi Tanya,

The code was intended as:

'<<=============
Private Sub Protect_Workbook_Open() '<<====
Dim ws As Worksheet
Const PWORD As String = ""BBHS" '<<====

For Each ws In Me.Worksheets
With ws
If .ProtectContents = False Then
.EnableSelection = xlUnlockedCells
.Protect password:=PWORD
End If
End With
Next ws

ActiveWorkbook.Protect password:=PWORD
End Sub
'<<=============

---
Regards,
Norman



Tanya

Protecting whole workbook
 
Thank you Norman!
It worked a charm.
Kind Regards
Tanya

Private Sub Protect_Workbook_Click() '<==== I am using a command button to
run this macro
'Protect workbook
Dim ws As Worksheet
Const PWORD As String = "BBHS"
For Each ws In ActiveWorkbook.Worksheets '<==== I replaced me with
ActiveWorkbook.
With ws
If ws.ProtectContents = False Then
.EnableSelection = xlUnlockedCells
.Protect Password:=PWORD
End If
End With
Next ws
ActiveWorkbook.Protect Password:=PWORD

End Sub

"Norman Jones" wrote:

Hi Tanya,

The code was intended as:

'<<=============
Private Sub Protect_Workbook_Open() '<<====
Dim ws As Worksheet
Const PWORD As String = ""BBHS" '<<====

For Each ws In Me.Worksheets
With ws
If .ProtectContents = False Then
.EnableSelection = xlUnlockedCells
.Protect password:=PWORD
End If
End With
Next ws

ActiveWorkbook.Protect password:=PWORD
End Sub
'<<=============

---
Regards,
Norman




Tanya

Protecting whole workbook
 
Thank you for your response Mike
Your solution didn't quite work as well as expected.
Kind Regards

"Mike H" wrote:

Very minor change to your code will do the trick:-

Private Sub Protect_Workbook_Click()
'Protect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = False Then
ws.Protect ("BBHS")
ActiveSheet.EnableSelection = xlUnlockedCells
End If
Next
ActiveWorkbook.Protect (["BBHS"])
Application.ScreenUpdating = True

Mike


End Sub

"Tanya" wrote:

Hi,
I have a workbook with approx 30 worksheets and have protected each
worksheet with the following macro's

Private Sub Protect_Workbook_Click()
'Protect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = False Then
ws.Protect ("BBHS")
End If
Next
ActiveWorkbook.Protect (["BBHS"])
Application.ScreenUpdating = True


End Sub

Private Sub UnProtect_Workbook_Click()
'Unprotect workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = True Then
ws.Unprotect ("BBHS")
End If
Next
ActiveWorkbook.Unprotect (["BBHS"])
Application.ScreenUpdating = True


End Sub

What I am finding is after I run the protection macro is each worksheet
allows you still to click in every cell. My question is, how do get the
macro to protect the worksheets in such a way that you can only click on a
cell that is unprotected?
If anyone could help with this I would be appreciate it greatly.
Regards
Tanya




All times are GMT +1. The time now is 05:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com