Use password to remove controls set by Macro

Closed
Kyle - Oct 17, 2011 at 12:10 PM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - Oct 20, 2011 at 08:59 AM
Hello,

I created a Macro that forces users to click "Enable Macro" or the rest of the sheets in the excel document will not populate (unhide). Also when they click "Enable MAcro" it removes the ability to Cut/Copy/Paste throughout the entire document.

How do I password enable the document so that when an authorized user types in a password it will allow them to Cut/Copy/Paste? Would like to do this in the form of a button.

Thanks!


Related:

1 response

TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 552
Oct 18, 2011 at 10:39 AM
HI Kyle,

See if this yields the desired result:

Create a button and goto the properties of the button.
Change (Name) into: cmdProtection
Change Caption into: Click here to protect all sheets

Still being in design mode, right-click the button and select view code.
Use this code:
Private Sub cmdProtection_Click()
Const PW As String = "Password"
Dim ws As Worksheet
Dim x As String

x = InputBox("Please provide password")

If cmdProtection.Caption = "Click here to protect all sheets" And x = PW Then
For Each ws In Worksheets
    ws.Protect Password:=PW, DrawingObjects:=True, Contents:=True, Scenarios:=True
        Next ws
cmdProtection.Caption = "Click here to unprotect all sheets"

ElseIf cmdProtection.Caption = "Click here to unprotect all sheets" And x = PW Then
For Each ws In Worksheets
    ws.Unprotect Password:=PW
        Next ws
cmdProtection.Caption = "Click here to protect all sheets"

    End If
End Sub

Now change everything/something/nothing between two double quotes ("") to suit your personal style.

Best regards,
Trowa
0
I definitely like the idea of the code that you have presented. However, I can not seem to get it to work but I would like to play off your code.

In my code I have:

Private Sub Workbook_Deactivate()
Call ToggleCutCopyAndPaste(True)
End Sub

how could we ues this to create a button that is password enabled to Activate the 'ToggleCutCopyPaste'

Kyle
0
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 552
Oct 20, 2011 at 08:59 AM
Hi Kyle,

Does this work for you?:

Private Sub cmdProtection_Click()
Const PW As String = "Password"
Dim x As String

x = InputBox("Please provide password")

If cmdProtection.Caption = "Click here to protect all sheets" And x = PW Then
Call ToggleCutCopyAndPaste(True)
cmdProtection.Caption = "Click here to unprotect all sheets"

ElseIf cmdProtection.Caption = "Click here to unprotect all sheets" And x = PW Then
Call ToggleCutCopyAndPaste(False)
cmdProtection.Caption = "Click here to protect all sheets"

    End If
End Sub


Have no idea if this would work for you since I do not know what code is behind "ToggleCutCopyAndPaste".

"Private Sub Workbook_Deactivate()" will make the code run when you exit the workbook and is no option when you want to use a button.

Best regards,
Trowa
0