Macro to Unprotect, Run a Sort, Then Re-protect... Please

Tricky

Board Regular
Joined
Jun 8, 2002
Messages
77
I have a sheet with data C3:N127. It is a named range: masterlist. Above the column headers, I have placed 10 arrows and have assigned sort macros to them. Such as:

Code:
Sub MacroJobName()
'
' 1/24/2015 by Ricky's 7
'
    Range("masterlist").Sort Key1:=Range("i3"), Order1:=xlAscending, Key2:=Range("C3") _
        , Order2:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:= _
        xlSortNormal
End Sub

The cells in the data sheet are locked, as the data is retrieved from other places, and the sheet is protected. Because the cells are locked, sorting is not allowed. I need help with code to unprotect the sheet, then call up a sort macro, then re-protect the sheet. Or, I could just add the on/off protection code to each of the ten sort macros. Suggestions certainly welcome.

Thanks,
Ricky
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Okay...

This wasn't as complicated as I first imagined, although there still may be a "cleaner" way to do this...

I added this line to the beginning of each Sort Macro that I had. This unprotects the sheet so the sort code can run. Even if the sheets were renamed, they still retain their original "Sheet#" name for coding purposes.

Sheet1.Unprotect Password:="***************"

Then, added: Sheet1.Protect Password:="***************" to the end of the code just prior to the End Sub line. This re-protects the sheet with your defined password.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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