Password in vba code

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?
 
H

Harald Staff

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
'
 
E

Earl Kiosterud

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

JE McGimpsey

Note that the UserInterfaceOnly switch is not persistent - you'll have
to set it every time the workbook is opened.
 
Top