+ Reply to Thread
Results 1 to 11 of 11

Password Protecting Sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-01-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2003
    Posts
    128

    Password Protecting Sheet

    I've found the code shown to Password Protcet a sheet, but I keep getting a Compile Error saying Variable Not Defined, can anyone help please?

    Option Explicit
    
    Private Sub Worksheet_Activate()
    Dim pwd As String:  pwd = "password"
    
    Cells(Rows.Count, Columns.Count).Activate
        If Application.InputBox("What is the password to view this sheet?", "Access Password", "???") <> pwd Then
            Sheets("Sheet1").Activate
            MsgBox "Wrong password"
        Else
            Range("A1").Activate
        End If
       
    End Sub

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Password Protecting Sheet

    Hi,

    This works fine for me.

    Do you have any other code in the worksheet module?
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Forum Contributor
    Join Date
    01-21-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    227

    Re: Password Protecting Sheet

    Quote Originally Posted by sweep View Post
    Hi,

    This works fine for me.

    Do you have any other code in the worksheet module?
    even for me it works perfectly fine.

    if possible attach your sheet, someone will look into it.
    If I've been of help, plz add reputation.

  4. #4
    Forum Contributor
    Join Date
    05-01-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: Password Protecting Sheet

    I can't attach sheet as I'm at work at the moment. Which sheet are you putting the code into? or does it go in a module?

  5. #5
    Forum Contributor
    Join Date
    01-21-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    227

    Re: Password Protecting Sheet

    Quote Originally Posted by nickhunt View Post
    I can't attach sheet as I'm at work at the moment. Which sheet are you putting the code into? or does it go in a module?
    Putting it into Sheet1

  6. #6
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Password Protecting Sheet

    Yes, as it's event driven (Private Sub Worksheet_Activate()) it needs to be in the worksheet where it will be used.

  7. #7
    Forum Contributor
    Join Date
    05-01-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: Password Protecting Sheet

    hi guys, I still can't see what I'm doing wrong, have attached a test file.
    Attached Files Attached Files

  8. #8
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Password Protecting Sheet

    Hi,

    This is quite odd. I got several errors while running your example sheet, but when I delete all the code and replace it manually (not cut and paste) it works. The only thing that I can guess is that you had unprintable characters in the code for some reason?

  9. #9
    Forum Contributor
    Join Date
    05-01-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: Password Protecting Sheet

    Hi Sweep,
    yeah, I found exactly the same thing, I guess you are right about some unprintable characters. I'll mark this as solved.
    Thanks again for your help

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,966

    Re: Password Protecting Sheet

    You had some Chr(160) characters (non-breaking spaces) - I suspect you pasted that code from a website?

    To clear it you can use Ctrl+H, then click in the Find box, hold Alt and type 0160 on the number keypad, then release Alt and press Replace All.
    Everyone who confuses correlation and causation ends up dead.

  11. #11
    Forum Contributor
    Join Date
    05-01-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: Password Protecting Sheet

    ok, thank you, something else learnt

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Protecting sheet password
    By John Cruz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-08-2014, 01:29 AM
  2. [SOLVED] Password Protecting a Hidden Sheet
    By JRose0303 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-25-2014, 12:30 PM
  3. Protecting sheet with password on close
    By a94andwi in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-24-2007, 08:11 PM
  4. Protecting a sheet with a password using VBA code
    By cgi_pro in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-05-2006, 05:16 AM
  5. [SOLVED] Sheet Protecting password
    By Nick in forum Excel General
    Replies: 1
    Last Post: 06-29-2005, 08:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1