VBA Coding to enable sorting but DISABLE filtering

owstie

New Member
Joined
Apr 29, 2016
Messages
2
Hello, I've trawled the net and found endless posts/replies about enabling filtering but disabling sorting, but couldn't find any about doing the opposite.

I've got the following code in the relevant sheet (which I want to be protected upon opening):

Private Sub Worksheet_Activate()
With Sheet4

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:= _
True, AllowFiltering:=True
End With
End Sub

This allows filtering, but if I try to sort A-Z (or Z-A) I get a message: "The cell or chart you are trying to change is on a protected sheet. To make changes, click Unprotect Sheet in the Review tab (you might need a password)."


If I change it to the following (which in my head should work):

Private Sub Worksheet_Activate()
With Sheet4

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:= _
True, AllowFiltering:=false
End With
End Sub

It just disables the filter buttons entirely.

So I have 2 problems. Firstly getting the sorting to work, and secondly disabling the ability to filter. Please help!

Thanks.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hello, I've trawled the net and found endless posts/replies about enabling filtering but disabling sorting, but couldn't find any about doing the opposite.

I've got the following code in the relevant sheet (which I want to be protected upon opening):

Private Sub Worksheet_Activate()
With Sheet4

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:= _
True, AllowFiltering:=True
End With
End Sub

This allows filtering, but if I try to sort A-Z (or Z-A) I get a message: "The cell or chart you are trying to change is on a protected sheet. To make changes, click Unprotect Sheet in the Review tab (you might need a password)."


If I change it to the following (which in my head should work):

Private Sub Worksheet_Activate()
With Sheet4

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:= _
True, AllowFiltering:=false
End With
End Sub

It just disables the filter buttons entirely.

So I have 2 problems. Firstly getting the sorting to work, and secondly disabling the ability to filter. Please help!

Thanks.
Hi owstie, welcome to the boards.

From Microsoft's own Office support website: "Sorting can only be performed on unlocked or unprotected cells in a protected worksheet."

This means that if the cells you want to sort have the "Locked" property when the sheet is protected then sort will not work.
 
Upvote 0

Forum statistics

Threads
1,216,488
Messages
6,130,952
Members
449,608
Latest member
jacobmudombe

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