Protecting/Unprotecting Worksheets using VBA/Macros

ammdumas

Active Member
Joined
Mar 14, 2002
Messages
469
I am trying to dummy-proof a worksheet and I want an easy way to protect
and unprotect the worksheets without having to go Tools --> Protection -->
etc.
I know how this can be done if the protection is used without the password,
but I WANT to use the password protection. Can code be written so that the
Password dialog box automatically pops up (when protecting AND
unprotecting)? Thanks in advance.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Do you want the password to popup ? because you can just use

ActiveSheet.Unprotect "mypassword"

or

ActiveSheet.Protect "mypassword"

to protect/unprotect the sheet with that password...
 
Upvote 0
Code:
Sub ProtectAllTest()

    Dim ws As Worksheet
Application.ScreenUpdating = False
    myPassword = InputBox("Enter password")
    For Each ws In ActiveWorkbook.Worksheets
    ws.Protect Password:=myPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True
    Next ws
    ActiveWorkbook.Protect Password:=myPassword, Structure:=True, Windows:=False
Application.ScreenUpdating = True

End Sub
 
Upvote 0
I want the user to have to enter in the password (while protecting or unprotecting. This is all happening while hiding and unhiding columns (that certain people shouldn;t see, hence the need for password protection). I have tried it in the past, but for some reason it just unprotects as if there was no password.
 
Upvote 0
Is it possible to have an input box that doesnt show the text when you type it in. For example, if my password was "jhol4" it would simply show up as *****?
 
Upvote 0
I agree with Jhol4. The code works (haven't tried it for unprotecting) but I would also like the text hidden.
 
Upvote 0
The InputBox does not provide for a "hidden" password character. You can accomplish this by building a simple userform. If you need more help on that, or would like examples, please let us know.
 
Upvote 0
jhol4 said:
Is it possible to have an input box that doesnt show the text when you type it in. For example, if my password was "jhol4" it would simply show up as *****?

If you're using Excel 2000 or Excel XP then you can use this.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top