"How do I un/protect sheet by macro program with password

M

Maruza

I need to input data only by macro programming.
Data sheet must be protected and only macro can unprotect to write data entry.
Then macro protects data sheet again to avoid any change.

I already try to protect with menu:
Tools > Protection > Protect sheet

But macro cannot remember the password.
Password is now always blank, which means can be unprotected by anybody.
 
J

JW

I need to input data only by macro programming.
Data sheet must be protected and only macro can unprotect to write data entry.
Then macro protects data sheet again to avoid any change.

I already try to protect with menu:
Tools > Protection > Protect sheet

But macro cannot remember the password.
Password is now always blank, which means can be unprotected by anybody.

To unprotect the sheet:
Sheets("Sheet2").Unprotect Password:="YourPassword"

To protect the sheet:
Sheets("Sheet2").Protect Password:="YourPassword"

To run a check and proceed accordingly:
Sub thiser()
Dim pw As String
pw = "YourPassword"
With Sheets("Sheet2")
If .ProtectContents = True Then
.Unprotect Password:=pw
Else
.Protect Password:=pw
End If
End With
End Sub
 
A

AR

The code given below will make it easy for anyone to learn the password if
they look in the code.

You can restrict access to your VB code by going to your project properties
(right click on your project in the project explorer window). Navigate to the
Protection tab and check the Lock project for viewing option. Enter a
password.

Anyone trying to access the VB code will require this password.
 
J

JW

The code given below will make it easy for anyone to learn the password if
they look in the code.

You can restrict access to your VB code by going to your project properties
(right click on your project in the project explorer window). Navigate to the
Protection tab and check the Lock project for viewing option. Enter a
password.

Anyone trying to access the VB code will require this password.

True, very true. It is always best to lock down VBA code IMO.
 
J

JE McGimpsey

Note that project protection is easily bypassed by techniques reely
available to anyone who has the ability to find these groups...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top