Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 287
Default Unprotecting and reprotecting within a macro

I have these codes which protect all my sheets in a workbook with the same
password

Sub LockEm()
Dim i As Long
Dim PW As String
Dim WS As Worksheet
PW = InputBox("Password:")
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Protect (PW)
Next
MsgBox i & " errors while protecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

Sub UnLockEm()
Dim i As Long
Dim PW As String
Dim WS As Worksheet
PW = InputBox("Password:")
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Unprotect (PW)
Next
MsgBox i & " errors while unprotecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

I wonder if it is possible to do the following:
1. First I Protect the sheets with the code in LockEm() and the password
qwerty
2. Then I have a macro called Macro1(). I cannot run this macro without
unprotecting the sheet since the macro refers to protected cells
Therefore I would add code to the Macro1() that in the beginning of the
sequence unprotect the sheet with the password entered above in the code
LockEm() i.e. PW. In this case the password is qwerty but that may differ
from time to time so therefore I cannot simply use unprotect("qwerty").
When I tried to enter the code unprotect(PW) VB did not recognize the
parameter PW. FYI the codes are in the same module.

Howcome the macro1() cannot recognize the PW but the code UnlockEm() can
(since it complains if I do not type in the same password as I did when I
protected the sheets)?
Is it at all possible to have a macro like:

Sub Macro1()
xxx.unprotect(PW)
' some code
xxx.protect(PW)
End sub

I have gotten some good hints but non of them has really solved my problem
Any help is deeply appreciated

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Unprotecting and reprotecting within a macro

Sub LockEmWithPW(PW as String)
Dim i As Long
Dim WS As Worksheet
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Protect PW
Next
MsgBox i & " errors while protecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

Sub UnLockEmWithPW(PW as String)
Dim i As Long
Dim WS As Worksheet
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Unprotect PW
Next
MsgBox i & " errors while unprotecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

Sub Macro1()
Dim PW As String
PW = InputBox("Password:")
If PW = "" then Exit Sub 'User cancelled?
UnLockEmWithPW PW

On error goto LockUp
'Do Something

LockUp:
LockEmWithPW PW
End Sub

Regards - Steve

PS, this wont work for blank passwords (If it must, let me know & I'll show you how to handle that)

"Adam" wrote in message ...
I have these codes which protect all my sheets in a workbook with the same
password

Sub LockEm()
Dim i As Long
Dim PW As String
Dim WS As Worksheet
PW = InputBox("Password:")
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Protect (PW)
Next
MsgBox i & " errors while protecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

Sub UnLockEm()
Dim i As Long
Dim PW As String
Dim WS As Worksheet
PW = InputBox("Password:")
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Unprotect (PW)
Next
MsgBox i & " errors while unprotecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

I wonder if it is possible to do the following:
1. First I Protect the sheets with the code in LockEm() and the password
qwerty
2. Then I have a macro called Macro1(). I cannot run this macro without
unprotecting the sheet since the macro refers to protected cells
Therefore I would add code to the Macro1() that in the beginning of the
sequence unprotect the sheet with the password entered above in the code
LockEm() i.e. PW. In this case the password is qwerty but that may differ
from time to time so therefore I cannot simply use unprotect("qwerty").
When I tried to enter the code unprotect(PW) VB did not recognize the
parameter PW. FYI the codes are in the same module.

Howcome the macro1() cannot recognize the PW but the code UnlockEm() can
(since it complains if I do not type in the same password as I did when I
protected the sheets)?
Is it at all possible to have a macro like:

Sub Macro1()
xxx.unprotect(PW)
' some code
xxx.protect(PW)
End sub

I have gotten some good hints but non of them has really solved my problem
Any help is deeply appreciated



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 287
Default Unprotecting and reprotecting within a macro

Thanks Stevie,
Only one problem, the executor of the macro will not know the password,
therefore I need the macro to call the PW that I entered and automatically
use it in Macro1()

"Stevie_mac" skrev:

Sub LockEmWithPW(PW as String)
Dim i As Long
Dim WS As Worksheet
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Protect PW
Next
MsgBox i & " errors while protecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

Sub UnLockEmWithPW(PW as String)
Dim i As Long
Dim WS As Worksheet
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Unprotect PW
Next
MsgBox i & " errors while unprotecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

Sub Macro1()
Dim PW As String
PW = InputBox("Password:")
If PW = "" then Exit Sub 'User cancelled?
UnLockEmWithPW PW

On error goto LockUp
'Do Something

LockUp:
LockEmWithPW PW
End Sub

Regards - Steve

PS, this wont work for blank passwords (If it must, let me know & I'll show you how to handle that)

"Adam" wrote in message ...
I have these codes which protect all my sheets in a workbook with the same
password

Sub LockEm()
Dim i As Long
Dim PW As String
Dim WS As Worksheet
PW = InputBox("Password:")
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Protect (PW)
Next
MsgBox i & " errors while protecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

Sub UnLockEm()
Dim i As Long
Dim PW As String
Dim WS As Worksheet
PW = InputBox("Password:")
On Error GoTo MyErr
For Each WS In ActiveWorkbook.Worksheets
WS.Unprotect (PW)
Next
MsgBox i & " errors while unprotecting", vbInformation
Exit Sub
MyErr:
i = i + 1
Resume Next
End Sub

I wonder if it is possible to do the following:
1. First I Protect the sheets with the code in LockEm() and the password
qwerty
2. Then I have a macro called Macro1(). I cannot run this macro without
unprotecting the sheet since the macro refers to protected cells
Therefore I would add code to the Macro1() that in the beginning of the
sequence unprotect the sheet with the password entered above in the code
LockEm() i.e. PW. In this case the password is qwerty but that may differ
from time to time so therefore I cannot simply use unprotect("qwerty").
When I tried to enter the code unprotect(PW) VB did not recognize the
parameter PW. FYI the codes are in the same module.

Howcome the macro1() cannot recognize the PW but the code UnlockEm() can
(since it complains if I do not type in the same password as I did when I
protected the sheets)?
Is it at all possible to have a macro like:

Sub Macro1()
xxx.unprotect(PW)
' some code
xxx.protect(PW)
End sub

I have gotten some good hints but non of them has really solved my problem
Any help is deeply appreciated




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unprotecting/Reprotecting Adam Excel Programming 3 April 28th 05 06:28 AM
Unprotecting worksheets with a macro Katrina Excel Programming 2 July 10th 04 09:59 AM
Macro for unprotecting/protecting worksheets WITH passwords John Baker Excel Programming 2 July 7th 04 01:19 PM
protecting vba code and unprotecting using a vba macro iain Excel Programming 0 June 29th 04 04:59 PM
Unprotecting worksheet in a macro. Mark F. Excel Programming 1 July 17th 03 08:38 PM


All times are GMT +1. The time now is 01:57 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"