ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How can the macro open a sheet that is locked with a password (https://www.excelbanter.com/excel-programming/275812-how-can-macro-open-sheet-locked-password.html)

Per Roar Øgaard

How can the macro open a sheet that is locked with a password
 
I make makros that can do a lot of things. I use the function for
protecting cells; where the code shall not be disturbed.
The makro have to unprotect the sheet; do the job; and then lock it again.
I want to use a password when i lock a sheet. That is simple.
But then the macro can't open it again.
Will somebody please tell me how to make a makro who unlocks a sheet with a
password.



Sub MyMakro()
ActiveSheet.Unprotect
'
'(I want a possibility to let the makro unprotect a sheet; who is protected
with a password)

' Here comes the VBA for what the makro shall do.

ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
ActiveSheet.EnableSelection = xlUnlockedCells
'(At least I want a possibility to let the makro protect the sheet with a
password)

End Sub

Thank you !!


Per R
Norway

--

Med hilsen

Per Roar Øgaard




Ron de Bruin

How can the macro open a sheet that is locked with a password
 
Sub test()
With Worksheets("sheet1")
.Unprotect Password:="hi"
MsgBox "place your code here"
.Protect Password:="hi"
End With
End Sub


Or better, use this

If you protect your sheet with code like this you can run macro's without unprotect first

Private Sub Workbook_Open()
With Worksheets("sheet1")
.Protect Password:="hi", userinterfaceonly:=True
End With
End Sub

Right click on the Excel icon next to File in the menubar
choose view code
paste it in there
Alt-Q to go back to Excel

It needs to be reset each time you open the workbook
that's why it is in the workbook open event

--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Per Roar Øgaard" wrote in message ...
I make makros that can do a lot of things. I use the function for
protecting cells; where the code shall not be disturbed.
The makro have to unprotect the sheet; do the job; and then lock it again.
I want to use a password when i lock a sheet. That is simple.
But then the macro can't open it again.
Will somebody please tell me how to make a makro who unlocks a sheet with a
password.



Sub MyMakro()
ActiveSheet.Unprotect
'
'(I want a possibility to let the makro unprotect a sheet; who is protected
with a password)

' Here comes the VBA for what the makro shall do.

ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
ActiveSheet.EnableSelection = xlUnlockedCells
'(At least I want a possibility to let the makro protect the sheet with a
password)

End Sub

Thank you !!


Per R
Norway

--

Med hilsen

Per Roar Øgaard






Rob Bovey

How can the macro open a sheet that is locked with a password
 
Hi Per,

To protect a sheet with a password you use:

ActiveSheet.Protect "MyPassword", True, True, True

To unprotect a sheet with a password you use:

ActiveSheet.Unprotect "MyPassword"

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"Per Roar Øgaard" wrote in message
...
I make makros that can do a lot of things. I use the function for
protecting cells; where the code shall not be disturbed.
The makro have to unprotect the sheet; do the job; and then lock it

again.
I want to use a password when i lock a sheet. That is simple.
But then the macro can't open it again.
Will somebody please tell me how to make a makro who unlocks a sheet with

a
password.



Sub MyMakro()
ActiveSheet.Unprotect
'
'(I want a possibility to let the makro unprotect a sheet; who is

protected
with a password)

' Here comes the VBA for what the makro shall do.

ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
ActiveSheet.EnableSelection = xlUnlockedCells
'(At least I want a possibility to let the makro protect the sheet with a
password)

End Sub

Thank you !!


Per R
Norway

--

Med hilsen

Per Roar Øgaard







All times are GMT +1. The time now is 12:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com