Sort a protected worksheet

srdavisgb

Board Regular
Joined
Nov 5, 2011
Messages
51
I have created a protected worksheet and saved the workbook with vba. Users must be able to sort the worksheet but, I don't want them to be able to change content in the locked cells (autofilter works). I have recorded the vba to "Allow Edit Ranges" and added to my code. It appears I have left something out...

I would appreciate any suggestions on how to correct the code. The code below is what I recorded that does not work:

ActiveSheet.Protection.AllowEditRanges.Add Title:="Autosort", Range:=Range( _"A1:U8000")
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True

This code will produce the desired workbook with protection on the worksheet but, the user can not sort. How do I modify this code to allow sorting?

ActiveSheet.Protect Password:="Winter2018", DrawingObjects:=False, Contents:=True, Scenarios:=True _
, AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True
ActiveSheet.EnableSelection = xlNoRestrictions


Thank you.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Please see the below from the AllowSorting section of the Worksheet.Protect Method from MSDN...

True allows the user to sort on the protected worksheet. Every cell in the sort range must be unlocked or unprotected. The default value is False .

P.S. in future can you please use Code Tags rather than colour your code red. To apply code tags paste the code in the thread, select the code and click the # icon.


Link to Worksheet.Protect Method
 
Upvote 0
Although the first code you posted with AllowEditRanges should work. What is it stating when you try the sort?
 
Upvote 0
Although the first code you posted with AllowEditRanges should work. What is it stating when you try the sort?

Thanks for your response. Actually, the sort does work. But, the cells that were locked can now be changed by the user. What I'm trying to do is create a worksheet that has all but three columns protected. The user can update only in these three columns. The code does password protect the worksheet and when I review the protection format the cells are still locked.

In regards to the statement, "True allows the user to sort on the protected worksheet. Every cell in the sort range must be unlocked or unprotected. The default value is False", what would be the purpose of protecting the worksheet if you are not able to lock the cells? I was hoping the "AllowEditRanges" code would unlock the protected cells, allow the sort to occur and re-protect the worksheet and lock the protected cells.

In any event, is there a way to sort a protected worksheet that has locked cells?
 
Upvote 0
Are you manually sorting the cells or via code? and if manually sorting is putting a button on the sheet and sorting by code an option? c

As for what is the purpose of protecting a sheet with the cells unlocked, the purpose is you only unlock the cells that you want affected you don't need to have every cell unlocked.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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