1
votes

I recorded the below VBA code that hide/unhide rows then sort on worksheet_Activate event, in no time :). Just one thing, the worksheet is required to be protected and this code refuse to be executed in a protected sheet and got this error message :

"run-time error 1004 "You cannot use this commend on a protected sheet..."

Could you please advise how we can protect the sheet and allow this code at the same time?

Sub HIDESORT()
    ActiveSheet.Range("$A$10:$K$500").AutoFilter Field:=7, Criteria1:=">0", _
        Operator:=xlAnd
    ActiveWorkbook.Worksheets("Control Implementation Plan").AutoFilter.Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("Control Implementation Plan").AutoFilter.Sort. _
        SortFields.Add Key:=Range("G10:G500"), SortOn:=xlSortOnValues, Order:= _
        xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Control Implementation Plan").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
2
when you protect the worksheet you've to allow users the possibility to hide/unhide cells by selecting the checkbox allow format columns. Try adding this segment to your code ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingColumns:=True psychicebola
Worked perfectly ;) thanksAhmed El Amrani

2 Answers

1
votes
ActiveSheet.Protect UserInterfaceOnly:=True
0
votes

when you protect the worksheet you've to allow users the possibility to hide/unhide cells by selecting the checkbox allow format columns. Try adding this segment to your code

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingColumns:=True