Protect worksheet without stopping a macro from running?

wrecclesham

New Member
Joined
Jul 24, 2019
Messages
45
I have a worksheet that I wish to enable protection on.

Unfortunately, whenever the worksheet is in a protected state and I update any values in any unlocked cells which the macro automatically applies sorting to, the following error appears:

gHoSNOX.png


Even if no cells have a "locked" attribute when I protect my worksheet, I still get this error whenever my macro attempts to automatically sort any dates entered, so it seems that enabling protection simply blocks some macro code from running, regardless of which cells are actually locked.

I've done some research and it seems that I need to use the following code in order to only lock the user interface, without preventing macros from running:

Code:
Me.Protect UserInterfaceOnly := True

Can someone please show me how I should add it to my existing code?

I have tried inserting the following before my existing code but it made no difference:

Code:
Public Sub AllowMacros()

Me.Protect UserInterfaceOnly:=True

End Sub

Here is my existing code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
 With Range("A1:A11")
      .Offset(, 4).Formula = "=IF(ISTEXT(D1),1,if(d1="""",9999999,D1))"
      .Resize(, 5).Sort key1:=Range("e1"), order1:=xlAscending, Header:=xlYes
      .Offset(, 4).Clear
   End With
Application.EnableEvents = True
    Range("A13:D18").Sort key1:=Range("D1"), _
      order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
    
    Range("A20:C30").Sort key1:=Range("A1"), _
      order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
    
End Sub
 
Last edited:

Some videos you may like

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

pwill

Active Member
Joined
Nov 22, 2015
Messages
406
If you right click the worksheet and choose protect sheet, in the dialog box that pops up, there's an option to Allow Sort. That might help?
 

Watch MrExcel Video

Forum statistics

Threads
1,127,625
Messages
5,625,946
Members
416,143
Latest member
JoyceMB

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
Top