Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Workbook vba protection with password

Posted on 2012-03-11
11
Medium Priority
?
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 37708299
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
ID: 37708304
Dave, thank you for your response. Whjat is meant by the save password?
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37708305
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.

 

Author Comment

by:rsen1
ID: 37708310
Sheets that need password and  EnableAutoFilter on
Labor
Roster
converter
input 1-20
input 21-40
0
 
LVL 42

Accepted Solution

by:
dlmille earned 2000 total points
ID: 37708317
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
 
LVL 42

Expert Comment

by:dlmille
ID: 37708321
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
ID: 37708334
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 42

Expert Comment

by:dlmille
ID: 37708335
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
ID: 37708342
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 42

Expert Comment

by:dlmille
ID: 37708349
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
ID: 37708351
Thank you Dave very much. You were a huge help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

636 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