Workbook vba protection with password

Please offer suggestions for Vba code to password protect a workbook with EnableAutoFilter = True added to the protectio.

Thank you
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Which sheets to do you want to password protect and add EnableAutoFilter on?

Or do you want to protect the entire workbook?  You really should do that with the save password, correct?

rsen1Author Commented:
Dave, thank you for your response. Whjat is meant by the save password?
let me ask again.

Do you just want to add filtering to the existing protected sheets, and also a password to those sheets?

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

rsen1Author Commented:
Sheets that need password and  EnableAutoFilter on
input 1-20
input 21-40
ok - I changed the code up to simplify (hopefully) and position you for more changes, protection options.  See the CASE statement, which articulates sheets that don't get touched, sheets that get touched with password/Filtering, and the rest which get the default protection that you had before.

Please advise if this is simpler to work with.

Here's the code:
Const myPassword = "password"
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim wkb As Workbook
Dim wks As Worksheet

    Set wkb = ThisWorkbook
    For Each wks In wkb.Worksheets
        Select Case UCase(wks.Name):
            Case "1-20 LABELS", "BLANK LABELS": 'exclude those in this list - put ALL IN UPPER CASE = CHECK SPACES AND SPELLING!!!
                'do nothing to these sheets
            Case "LABOR", "ROSTER", "CONVERTER", "INPUT 1-20", "INPUT 21-40": 'one sheet name per quotation marks
                'put any different worksheet protection code, here
                wks.Protect Password:=myPassword, AllowFiltering:=True
            Case "finallyOne more sheet", "other sheet", "that get the same protection": 'one sheet name per quotation marks
                'for future use, if you want different type of protection
                'put any different (from the above)  worksheet protection code, here
            Case Else: 'all others, just do this protect approach
                wks.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFormattingCells:=True
        End Select
    Next wks

End Sub

Open in new window

See attached.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Note, the password is hardcoded in the code (first line), so pretty easy for users to get at, but if this is just a reminder that they don't have access and you want to trouble them not to unlock the sheet, then you're ok.  Excel/Office is fairly notorious for weak security when it comes to stuff like this.


rsen1Author Commented:
Dave that works great, thank you.

Why would l not want this workbook set to this password code on open and if it were set for workbook change would have the same effect on the copy paste as it did sitting on the sheet code?
I'm not sure exactly what you said. I was just saying that any user can go to VBA to see the password.  Do you need more security than this?

rsen1Author Commented:
I can set the vba password.

I was curious for the reason to set this password in
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
rather than
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Why continuously set the password?  SheetChange would do it everytime the cursor changed from one cell to the next.

it really only need be set by the person that has password access to change the format of the workbook.  Once everything is protected, no code really need be run afterward.  In fact, you could protect everything from another workbook and this one have no code at all, lol.

However, what this allows the way its currently coded, is for you to unprotect a tab manually, then when the workbook is saved, everything is made under wraps again.

It really doesn't matter that that code runs when the user saves (because everything is ALREADY protected), its just redundant at that point.

Make sense?

rsen1Author Commented:
Thank you Dave very much. You were a huge help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.