Macro to Protect All except a few Ranges

SamAnnElizabeth

New Member
Joined
Mar 15, 2013
Messages
34
Hey all,

I think this should be pretty straight forward, but I'm struggling a little. (I'm still learning VBA.) I'm trying to protect a workbook, but leave a few ranges available for co-workers to edit. I have the ranges set up so I can go to: Review Ribbon: Changes Group: Allow Users to Edit Ranges. (Here my ranges are already define) Protect Sheet... (I have my permissions set up already) OK. I'd like to automate this process so it is performed when the sheet is closed. I've found how to protect a while workbook, but I want to do all except my defined ranges. Is there a VBA template someone can show me to follow?

Thanks guys! This forum has been very helpful.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Ok, I think I'm close, but that thread was not what I was looking for. Here's a rough version of my VBA code, can you tell me what I need to change?

Sub Protect()
'
' Protect Macro
' Protects the workbook upon closing.
'
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, cancel As Boolean)
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=False, AllowUsingPivotTables:=True
Range(Actual_Date, Comments).Select
Selection.Locked = False
Sheets("Review_Dates").Select
ActiveWindow.SelectedSheets.Visible = False
End Sub


"Actual_Date" and "Comments" are some named ranges I have. I get a 'compile error' when I try to run this. Help!
 
Upvote 0
Ok, I think I'm close, but that thread was not what I was looking for. Here's a rough version of my VBA code, can you tell me what I need to change?

Sub Protect()
'
' Protect Macro
' Protects the workbook upon closing.
'
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, cancel As Boolean)
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=False, AllowUsingPivotTables:=True
Range("Actual_Date", "Comments").Select
Selection.Locked = False
Sheets("Review_Dates").Select
ActiveWindow.SelectedSheets.Visible = False
End Sub


"Actual_Date" and "Comments" are some named ranges I have. I get a 'compile error' when I try to run this. Help!

Is that what you get when you use the macro recorder?

I do believe you require quotation marks " " (see above)around Actual_Date, Comments
 
Upvote 0
No, it's what I wrote based on the information you provided me in the link. The debugger is saying that there's a problem with me having 'Sub Protect()' at the beginning and 'Private Sub' later down. Can you help me with this?
 
Upvote 0
Oh yes, remove the sub protect() and makesure the rest of the code is in the THIS WORKBOOK section and not as a MODULE. I don't think you require any of this code though.

Rich (BB code):
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=False, AllowUsingPivotTables:=True
 
Upvote 0
I don't know what I'm doing wrong. I have this as my VBA and when I run it, the dialogue box to select a macro comes up, and my Macro is not on the list! Here's what I'm doing:



'
' Protect Macro
' Protects the workbook upon closing.
'
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, cancel As Boolean)
Range("Actual_Date", "Comments", "Current_Gateway", "Rating", "Uncontained_Issues").Select
Selection.Locked = False
Sheets("Review_Dates").Select
ActiveWindow.SelectedSheets.Visible = False
End Sub
 
Upvote 0
Ok, this part
Rich (BB code):
Range("Actual_Date", "Comments", "Current_Gateway", "Rating", "Uncontained_Issues").Select


needs to be
Rich (BB code):
Range("Actual_Date", "Comments").select
Range("Current_Gateway", "Rating").select
Range("Uncontained_Issues").Select


I assume you would need some code to make the Review_Dates sheet visable?

Also it states that you require the spreadsheet to be protected once it is closed so you will need instead of
Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, cancel As Boolean)

This
Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)


 
Last edited:
Upvote 0
Here is my code that I have tested - adjust ranges/sheets to suit

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Range("A1", "b2").Select
Range("c3", "d4").Select
Selection.Locked = False
Sheet(2).Select
ActiveWindow.SelectedSheets.Visible = False
End Sub
 
Upvote 0
I don't think this does at all what I need. I need it to protect the rest of the workbook, this only unprotects the selected ranges, which wouldn't be locked in the first place. How can I be sure to hide other sheets? I think I need to make them visible first?
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top