Allowing sorting on a protected sheet

STEVENS3010

Board Regular
Joined
Feb 4, 2020
Messages
89
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

I have a code where it allows filtering on a protected sheet but when I'm trying to add the allow sorting feature I get the 'cannot edit protected sheet error message'.

The VBA code I'm using is below - I cannot see where I'm going wrong; can anybody help?

VBA Code:
With ActiveSheet
    .Protect Password:="", AllowFiltering:=True, AllowSorting:=True
End With
 

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
You have to unlock the sort range
VBA Code:
With ActiveSheet
    .Range("A1:A100").Locked = False                  'unlock the cells you want to sort
    .Protect Password:="", AllowFiltering:=True, AllowSorting:=True
End With
 
Upvote 0
Thanks for the reply. The issue I have is that is the data I want to keep protected.
 
Upvote 0
Well the idea behind a protected sheet is to restrict modification, and sorting is modification from the perspective of an individual cell. You have a couple of options. One is to control sorting via VBA. That way you can protect and unprotect as needed. The other is to create a copy of the table or range and allow sorting on the copy, not the original. You could also unlock the cells, but then prevent any cells from being selected on the protected sheet.
 
Upvote 0
Thanks for your advice. None of those options really work for the type of sheet I have. I guess I'll just have to live without the sort function. It's really weird as the filter option works but the sort doesn't?
 
Upvote 0
It's really weird as the filter option works but the sort doesn't?
Not so weird. The filter function does not modify data in a given cell. It just displays it or not. When you sort, you rearrange data. You may not think of it as changing the data, but the act of sorting changes the contents of cells.
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,022
Members
449,203
Latest member
tungnmqn90

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