Protect worksheet without stopping a macro from running?

wrecclesham

Board Regular
Joined
Jul 24, 2019
Messages
52
Office Version
  1. 365
Platform
  1. Windows
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:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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?
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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