Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Use a password in VBA ActiveSheet.protect & ActiveSheet.unprotect?

I have about a dozen worksheets in a workbook that are password protected. I
want to use a macro to unprotect each sheet (one at a time), lock or unlock
some cells, and then password protect the worksheet before moving to the next
sheet. The password is the same for all the worksheets. I would like the
macro to prompt one time for the password and then use that input to perform
the unprotect and protect. I got the VBA code below when I recorded a macro.
Unfortunately, using this code, the user is prompted each time the worksheet
is unprotected, the cell locks are set, and then the worksheet is protected
again but without the password.

Sheets("Special Loans").Select
ActiveSheet.Unprotect
Cells.Select
Range("M1").Activate
Selection.Locked = True
Selection.FormulaHidden = False
Range("A1").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default Use a password in VBA ActiveSheet.protect & ActiveSheet.unprotect?

Hi Jim,

Try something like:

'===========
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim PWORD As String

PWORD = InputBox(Prompt:="Insert password")

Set WB = ThisWorkbook

For Each SH In WB.Worksheets
With SH
On Error GoTo XIT
.Unprotect PWORD
With .Range("M1")
.Locked = True
.FormulaHidden = False
End With
.Protect password:=PWORD, _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True
End With
Next SH
WB.Save
Exit Sub

XIT:
MsgBox Prompt:="Password not recognised"
End Sub
'<<===========



---
Regards.
Norman


"Jim K." <Jim wrote in message
...
I have about a dozen worksheets in a workbook that are password protected.
I
want to use a macro to unprotect each sheet (one at a time), lock or
unlock
some cells, and then password protect the worksheet before moving to the
next
sheet. The password is the same for all the worksheets. I would like the
macro to prompt one time for the password and then use that input to
perform
the unprotect and protect. I got the VBA code below when I recorded a
macro.
Unfortunately, using this code, the user is prompted each time the
worksheet
is unprotected, the cell locks are set, and then the worksheet is
protected
again but without the password.

Sheets("Special Loans").Select
ActiveSheet.Unprotect
Cells.Select
Range("M1").Activate
Selection.Locked = True
Selection.FormulaHidden = False
Range("A1").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
ActiveWorkbook.Save


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Use a password in VBA ActiveSheet.protect & ActiveSheet.unprot

That was just the code I was looking for. THANKS!!!

"Norman Jones" wrote:

Hi Jim,

Try something like:

'===========
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim PWORD As String

PWORD = InputBox(Prompt:="Insert password")

Set WB = ThisWorkbook

For Each SH In WB.Worksheets
With SH
On Error GoTo XIT
.Unprotect PWORD
With .Range("M1")
.Locked = True
.FormulaHidden = False
End With
.Protect password:=PWORD, _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True
End With
Next SH
WB.Save
Exit Sub

XIT:
MsgBox Prompt:="Password not recognised"
End Sub
'<<===========



---
Regards.
Norman


"Jim K." <Jim wrote in message
...
I have about a dozen worksheets in a workbook that are password protected.
I
want to use a macro to unprotect each sheet (one at a time), lock or
unlock
some cells, and then password protect the worksheet before moving to the
next
sheet. The password is the same for all the worksheets. I would like the
macro to prompt one time for the password and then use that input to
perform
the unprotect and protect. I got the VBA code below when I recorded a
macro.
Unfortunately, using this code, the user is prompted each time the
worksheet
is unprotected, the cell locks are set, and then the worksheet is
protected
again but without the password.

Sheets("Special Loans").Select
ActiveSheet.Unprotect
Cells.Select
Range("M1").Activate
Selection.Locked = True
Selection.FormulaHidden = False
Range("A1").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
ActiveWorkbook.Save


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
Copying new activesheet after other activesheet is hidden? Simon Lloyd[_790_] Excel Programming 1 June 20th 06 10:02 AM
Difference between Activesheet.unprotect Dennis Excel Discussion (Misc queries) 2 August 24th 05 01:27 PM
ActiveSheet.Unprotect Souris Excel Programming 0 August 22nd 05 12:32 AM
ActiveSheet.Protect Mike Excel Programming 2 May 25th 05 10:33 PM
ActiveSheet.Protect Żaczek Excel Programming 1 September 27th 04 12:21 PM


All times are GMT +1. The time now is 03:44 AM.

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

About Us

"It's about Microsoft Excel"