Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1018783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:54:37+00:00 2026-05-16T10:54:37+00:00

I have locked cells in a spreadsheet using this lines of code. Range(A1:D23).Select Selection.Locked

  • 0

I have locked cells in a spreadsheet using this lines of code.

Range("A1:D23").Select
Selection.Locked = True
ActiveSheet.Protect Contents:=True

This prompts me whenever I click on a cell which is readonly to unprotect sheet from review tab and might be prompted for password.

My problem is, it is not prompting for password.How do I first set password when he wants to unprotect.Secondly I want to pass the row information that he selected to change and want to create a button when the adjoining readonly cell is used for editing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:54:37+00:00Added an answer on May 16, 2026 at 10:54 am

    I get the first part of your question. You need to specify a password argument as part of the worksheet’s Protect method:

    Sub lockSheet()
        With ActiveSheet
    
            'Check sheet is not already protected
            If Not .ProtectContents Then
    
                'Clear any existing lock settings
                .Cells.Locked = False
    
                .Range("A1:D23").Locked = True
                .Protect Contents:=True, Password:="pw"
            End If
        End With
    End Sub
    

    Note that it is not necessary to select a range before modifying it, simply apply the action directly to the range.

    The password is obviously visible in plaintext, if you need to secure to any degree then you’ll also need to apply a password to the VBA project.

    To address the second part of your question, as amended in your comment:

    Private Sub Worksheet_SelectionChange(ByVal target As Range)
        If target.Locked Then
            With ActiveSheet
                .Unprotect
                If Not .ProtectContents Then
                    target.Locked = False
                    .Protect Contents:=True, Password:="pw"
                End If
            End With
        End If
    End Sub
    

    This would need added to the worksheet module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have sample code to copy open (in-use and locked by another program)
I have a file on a Windows 2003 server which is locked by a
I have looked around on the Internet trying to answer this question. It seems
I have looked all of the place for this and I can't seem to
I'm trying to select the used range. I get the worksheet's UsedRange, then I
I have a form being displayed by a static method. This static method calls
I have looked over the Repository pattern and I recognized some ideas that I
I have looked at the SQL Server 2008 feature comparison matrix and it lists
I have looked at NHibernate and EntitySpaces and they both seem to work differently.
I have looked on FaceBook Developer page and found that it's possible to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.