Excel/Password Code



I need some help please, i created this code for a workbook and having an issue

what i want to do is be able to change the password on a sheet without having to go into coding and change it.  I am able to get it to work on a normal code such as this :

Option Compare Text
'Password to unhide sheets
Const pWord = "lt"

but the following code doesn't seem to work ....it shows me an error of course if i simply put in a password it will work, but i want to be able to change the password if needed directly from the cell on the specific page.i know its something simple if you could take a look i would really appreciate it....

Option Compare Text
'Password to unhide sheets
Const pWord:=Sheet33.Range("w1")

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   Cancel = True

Application.ScreenUpdating = False

   If Not Intersect(Target, Range("j14")) Is Nothing Then

   Select Case InputBox("Please enter the password to unhide the sheet", _
       "Enter Password")
       Case Is = pWord
         With Worksheets("Master")
         .Visible = xlSheetVisible
   On Error Resume Next
   ActiveSheet.Protect Password:=Sheet33.Range("w1")
   ActiveWindow.SelectedSheets.Visible = False
   Worksheets("00").Visible = xlSheetVeryHidden
         End With
       Case Else
         MsgBox "Sorry, that password is incorrect!", _
         vbCritical + vbOKOnly, "You are not authorized!"
   End Select
   End If

End Sub

this code if placed in any codes works fine....

On Error Resume Next
   ActiveSheet.Protect Password:=Sheet33.Range("w1")


this statement is  your problem

Const pWord:=Sheet33.Range("w1")

you are declaring pWord a constant and then treating it as a variable.  Just declare it as a variable and use it as a variable or reference it directly.

Dim pWord as Variant

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Cancel = True
  pWord = Sheet33.Range("W1").Value

  'rest of your code
Application.ScreenUpdating = False

or just skip pWord and use

Case Is = Sheet33.Range("W1").Value

Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 About.com. All rights reserved.