• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

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
0
rsen1
Asked:
rsen1
  • 6
  • 5
1 Solution
 
dlmilleCommented:
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?

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

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

Dave
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rsen1Author Commented:
Sheets that need password and  EnableAutoFilter on
Labor
Roster
converter
input 1-20
input 21-40
0
 
dlmilleCommented:
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.

Dave
protect-r2.xlsm
0
 
dlmilleCommented:
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.

Cheers

Dave
0
 
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?
Robert
0
 
dlmilleCommented:
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?

Dave
0
 
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)
0
 
dlmilleCommented:
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?

Dave
0
 
rsen1Author Commented:
Thank you Dave very much. You were a huge help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now