Establishing Password Protection on WS to protect Macros and Formulas

EE Pros,  I have a GREAT WS that Martin Liss has been instrumental in helping me get it to this point.  I'm attempting to now establish password protection on the cells/fields so as to insure the formulas and macros do not get messed up.  I've attached the WB/WS for your review.  It has a Private Sub that triggers a checkmark in Col. A that doesn't work if I protect the WS.  I have Unlocked all of the cells that I want to make accessible and all others are coded for lock when password protection is on.  My understanding is that I may be able to issue a macro/command that will allow macros to do what they need to do within a WS without having to code each one.

Thank you in advance.

B.
Tempv8Protect.xlsm
Bright01Asked:
Who is Participating?

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

x
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.

Martin LissOlder than dirtCommented:
In Workbook_Open do the following, changing the sheet name and password as appropriate. You can add multiple lines for multiple sheets.

Private Sub Workbook_Open()
Sheets("My sheet name").Protect Password:="My Password", UserInterFaceOnly:=True
End Sub

Open in new window


Then Format->unprotect the ranges that you want the user to be able to change.

Two things to note:
1) You’ll need to remove all other existing password protect/unprotect code, and
2) You’ll need to close and reopen the workbook or run the Workbook_Open sub manually for the new protection to take effect.

If you have a lot of sheets and you want them all to be protected

Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
    Sheets(ws.Name).Protect Password:="My Password", UserInterFaceOnly:=True
Next
End Sub

Open in new window

Bright01Author Commented:
Martin,

Thanks!  Do I put this Private Sub in the WS or "This Workbook" or a Module?

I don't know what it means to say "In Workbook_Open........"

B.
Martin LissOlder than dirtCommented:
In the Visual Basic area if you double-click on ThisWorkbook you will either see this
One
or this depending on what you did last (and you may not see "Open").
Two
In it's the latter and you don't see "Open" click on the drop-down on the right and you'll see all the events associated with the workbook. Select "Open" if not already selected and paste my code there.

If it's the former, first select the drop-down on the left and select "Workbook".
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Bright01Author Commented:
OK... did that.  And it almost worked.  In other words, it worked for all Macros except with "Details" and "Summary" macro.  I get an error 400 when trying to expand the outline format.

B.
Tempv9Protect.xlsm
Martin LissOlder than dirtCommented:
You could always add an ActiveSheet.Unprotect Password:="My Password" at the start of each of those and an ActiveSheet.Protect Password:="My Password", UserInterFaceOnly:=True at the end of each but let me look into it.
Martin LissOlder than dirtCommented:
Try this

Private Sub Workbook_Open()
Sheets("Assets").EnableOutlining = True
Sheets("Assets").Protect Password:=""My Password", UserInterFaceOnly:=True
End Sub

Open in new window

Bright01Author Commented:
When I replaced the previous with the new, recommended, the other macros locked up.

Not working for some reason.

B.
Martin LissOlder than dirtCommented:
Please repost your workbook with the amended Open code.
Bright01Author Commented:
Wait.... I got it to work by insuring the passwords were consistent.  But the outline expansion capability is still not working.

B.
Martin LissOlder than dirtCommented:
What do you mean by "I got it to work by insuring the passwords were consistent"? As I mentioned in my first post he only reference to a password should be in the Open code.
Bright01Author Commented:
Here you go!
Tempv9Protect.xlsm
Bright01Author Commented:
Password is "pass"

I removed the references before testing.  The problem is still the outline.

B.
Martin LissOlder than dirtCommented:
In this sub there's a problem.
Sub button_Click()
Const SumBtnCaps As String = "Summary, Details"
Const ShowLevels As String = "1,2"

    Dim vMatch
    With ActiveSheet.Buttons(Application.Caller)

        vMatch = Application.Match(.Caption, Split(SumBtnCaps, ","), 0)
        If Not IsError(vMatch) Then
            ActiveSheet.Outline.ShowLevels RowLevels:=CLng(Split(ShowLevels, ",")(vMatch - 1))
            'Me.Outline.ShowLevels RowLevels:=CLng(Split(ShowLevels, ",")(vMatch))
'            .Caption = Split(SumBtnCaps, ",")(vMatch Mod 2)
        End If
    End With
'This sub expands or contracts the outline using the button to fire the macro
    End Sub

Open in new window


If you comment out line 12 as I did, you don't get the strange 400 error and nothing seems different.
Bright01Author Commented:
Martin,

If you comment out line 12, the Outline Macro does not work (check out the Summary/Detail button).

Can you test the Summary/Detail button and send me your copy with changes?  The button changes to reflect the condition and expands and contracts the outline capability.

B.
Martin LissOlder than dirtCommented:
Well I didn't see anything that didn't work but you're certainly more familiar with the sheet than I am. In any case add lines 12 and 14.
Sub button_Click()
Const SumBtnCaps As String = "Summary, Details"
Const ShowLevels As String = "1,2"

    Dim vMatch
    With ActiveSheet.Buttons(Application.Caller)

        vMatch = Application.Match(.Caption, Split(SumBtnCaps, ","), 0)
        If Not IsError(vMatch) Then
            ActiveSheet.Outline.ShowLevels RowLevels:=CLng(Split(ShowLevels, ",")(vMatch - 1))
            'Me.Outline.ShowLevels RowLevels:=CLng(Split(ShowLevels, ",")(vMatch))
            On Error Resume Next
            .Caption = Split(SumBtnCaps, ",")(vMatch Mod 2)
            On Error GoTo 0
        End If
    End With
'This sub expands or contracts the outline using the button to fire the macro
    End Sub

Open in new window

Bright01Author Commented:
Martin,

I added all of the changes you recommended.  There is still a problem.

Please open the workbook.  It will be Locked.  All the Macros seem to work except the Detail/Summary button and associated Macro.  If you fire it now, you don't get a protect error, it collapses the outline, but then doesn't expand when fired a second time.  You have to unlock the sheet to get the macro to work again.

Any ideas?

Thank you,

B.
Tempv10Protect.xlsm
Martin LissOlder than dirtCommented:
Private Sub Workbook_Open()
Sheets("Assets").EnableOutlining = True
Sheets("Assets").Protect DrawingObjects:=False, _
                         Contents:=True, _
                         Scenarios:=True, _
                         Password:="pass", _
                         UserInterFaceOnly:=True
End Sub

Open in new window

Bright01Author Commented:
I tried this code in the Assets WS and the This Workbook section.  Is that the right place to have placed it?  Still got an error code 400.

B.
Martin LissOlder than dirtCommented:
No, just ThisWorkbook. Workbook attached.
28840599.xlsm

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
Bright01Author Commented:
Martin,

Great work!  You are a pleasure to work with and a great teacher.   If you were my next door neighbor, I'd be taking you to lunch for all your help and support.  Thank you again for such effort and results.

Best regards,    B.
Martin LissOlder than dirtCommented:
<blush> You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest youincluding these two new ones.
An Experts Exchange Shortcut for the Truly Lazy (for Apple OS X)
A Guide to Writing Understandable and Maintainable VBA Code
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
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.