Password in vba code

  • Thread starter Thread starter MikeyMay
  • Start date Start date
M

MikeyMay

I have a worksheet that I want to protect but needs
amending and saving when a macro is running. how do I
unprotect using a password and re protect using same
password in my code?
 
Sub test()
With Sheets(1)
..Unprotect Password:="Jolene"
..Cells(1, 1).Locked = True
..Cells(1, 1).Value = Time
..Columns(1).AutoFit
..Protect Password:="Jolene"
End With
End Sub

HTH. Best wishes Harald
'
 
Mikey,

If you're unprotecting it for the user to make changes, unprotect it in
code. If you're wanting a macro to be able to make changes, you can protect
it against the user, but not against the macro with

Sheets("Whatever").Protect UserInterFaceOnly:=True, Password:="MyPass"

Now the code can mess with the sheet, but the user can't.
 
Note that the UserInterfaceOnly switch is not persistent - you'll have
to set it every time the workbook is opened.
 

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

Back
Top