0
votes

Using Excel VBA, how do I lock worksheets with multiple different variables i.e. Allowing users to use Autofilter and the sort option.

I have gotten this far:

Worksheets("Sheet 1").Protect Password:="Password"

What do I write next to allow user to: Select un/locked cells Format columns/rows Sort Use Autofilter

Thank you,

2
Unprotect it I guessStupid_Intern

2 Answers

0
votes

There are 16 parameters you can use with the Protect method of the worksheet. Here is a code sample based off this MSDN article:

Option Explicit

Sub LockSheet()

    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets(1)

    ws.Protect Password:="Foo", _
        DrawingObjects:=True, _
        Contents:=True, _
        Scenarios:=True, _
        UserInterfaceOnly:=True, _
        AllowFormattingCells:=True, _
        AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, _
        AllowInsertingColumns:=True, _
        AllowInsertingRows:=True, _
        AllowInsertingHyperlinks:=True, _
        AllowDeletingColumns:=True, _
        AllowDeletingRows:=True, _
        AllowSorting:=True, _
        AllowFiltering:=True, _
        AllowUsingPivotTables:=True

End Sub
0
votes

Just to add to the answer given by @Robin, here is the URL to the .Protect function, you may find it usefull to read what each of the parameters do, also some are assumed to be true and some to be false.

https://msdn.microsoft.com/en-us/library/office/ff840611.aspx

Kind regards,

lewisthegruffalo