Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Protection Password Prompt Problem

I would like the prompt the user to input a password if the password
string variable does not already have a value.

This is the code I currently have.

Private Sub Worksheet_Activation(ByVal Target As Excel.Range)
Dim PWORD As String
If PWORD Is Nothing Then
PWORD = InputBox("Enter protection password:", "Set
Protection Password")
End If
ActiveWorkbook.Protect Password:=PWORD, Structu=True,
Windows:=True
ActiveSheet.Protect Password:=PWORD, DrawingObjects:=True,
Contents:=True, Scenarios:=True
ActiveSheet.EnableSelection = xlNoSelection
Application.DisplayFormulaBar = False
End Sub

Perhaps I should move the prompt to Sheet1_Initialize()?

Another problem I am having is that the PWORD string variable isn't
being recognized by .Protect and .Unprotect.

I have tried:

ActiveWorkbook.Protect Password:=PWORD, Structu=True, Windows:=True

and

ActiveWorkbook.Protect (PWORD, True, True)

They both do not work. Likewise for the ActiveWorkbook.Unprotect




I have only about a week's worth of self-VBA training. haha

Regards,
Tim
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Protection Password Prompt Problem

There are several problems in your code. First, there is no such event
as

Private Sub Worksheet_Activation(ByVal Target As Excel.Range)

There is a

Private Sub Worksheet_Activate()
event and a
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
event.

You can't just create events that you think might be useful. (Well,
you can create events, but that isn't relevant here.)

You have defined PWORD as a String variable, so you cannot use the "Is
Nothing" test on it. "Is Nothing" applies only to objects, not
fundamental data types. The code:

Dim PWORD As String
If PWORD Is Nothing Then


won't work and is meaningless.

Since you test PWORD immediately after declaring it:

Dim PWORD As String
If PWORD Is Nothing Then


or, syntactically correctly,

Dim PWORD As String
If PWORD = vbNullString Then


PWORD will always be empty since no code has executed that could have
possibly put a value in it.

Perhaps I should move the prompt to Sheet1_Initialize()?


Again, there is no such event. Sheet1 always exists, so there is no
point in the sequence of events that it would be initialized.

You best bet might be to use the Macro Recorder (Tools menu, Macros
item, Macro Recorder) to record protecting and unprotecting
worksheets. Macro recorder code is pretty poor and can't (or at least
shouldn't) be used in real code, but it does show you what objects and
which properties and methods of those objects are used to accomplish a
task.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)

On Fri, 2 Jan 2009 13:22:22 -0800 (PST), wrote:

I would like the prompt the user to input a password if the password
string variable does not already have a value.

This is the code I currently have.

Private Sub Worksheet_Activation(ByVal Target As Excel.Range)
Dim PWORD As String
If PWORD Is Nothing Then
PWORD = InputBox("Enter protection password:", "Set
Protection Password")
End If
ActiveWorkbook.Protect Password:=PWORD, Structu=True,
Windows:=True
ActiveSheet.Protect Password:=PWORD, DrawingObjects:=True,
Contents:=True, Scenarios:=True
ActiveSheet.EnableSelection = xlNoSelection
Application.DisplayFormulaBar = False
End Sub

Perhaps I should move the prompt to Sheet1_Initialize()?

Another problem I am having is that the PWORD string variable isn't
being recognized by .Protect and .Unprotect.

I have tried:

ActiveWorkbook.Protect Password:=PWORD, Structu=True, Windows:=True

and

ActiveWorkbook.Protect (PWORD, True, True)

They both do not work. Likewise for the ActiveWorkbook.Unprotect




I have only about a week's worth of self-VBA training. haha

Regards,
Tim

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
Excel Data Protection- AKA: Sheet/Macro Password Protection Mushman(Woof!) Setting up and Configuration of Excel 0 December 29th 09 06:50 AM
password protection problem dpov Excel Discussion (Misc queries) 2 August 19th 09 10:22 PM
Password Protection Problem Duncan, UK Excel Discussion (Misc queries) 0 September 14th 06 03:53 PM
Prompt for protection password when not trying to access VBA Jamie B Excel Discussion (Misc queries) 0 July 13th 06 08:13 PM
Excel 2003 Userinterfaceonly Password Protection Problem [email protected] Excel Programming 1 June 8th 05 06:13 PM


All times are GMT +1. The time now is 01:08 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"