Solved

Workbook vba protection with password

Posted on 2012-03-11
11
512 Views
Last Modified: 2012-03-11
Please offer suggestions for Vba code to password protect a workbook with EnableAutoFilter = True added to the protectio.

Thank you
0
Comment
Question by:rsen1
  • 6
  • 5
11 Comments
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
 

Author Comment

by:rsen1
Comment Utility
Dave, thank you for your response. Whjat is meant by the save password?
0
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
 

Author Comment

by:rsen1
Comment Utility
Sheets that need password and  EnableAutoFilter on
Labor
Roster
converter
input 1-20
input 21-40
0
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
 

Author Comment

by:rsen1
Comment Utility
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
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
 

Author Comment

by:rsen1
Comment Utility
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
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
 

Author Comment

by:rsen1
Comment Utility
Thank you Dave very much. You were a huge help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now