+ Reply to Thread
Results 1 to 25 of 25

Lock and protect selected excel rows with VBA code

  1. #1
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Question Lock and protect selected excel rows with VBA code

    Hi Gurus,

    I've an excel worksheet with sama data to be protected and some data to be updated by our users on a regular basis.

    My query is, will it be possible to use VBA code to password protect any selected row after updating? Meaning, once the user has finished updating that particular row, maybe with a click of a button, that row will be procteted and no additional change allowed. Only admin who has the password will be able to make any additional changes.

    However, the rest of the row will still be accessible and updated by user till it's protected.

    Appreciate for your help.

    Thanks.


    - Jack
    Regards,
    Jack

  2. #2
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Lock and protect selected excel rows with VBA code

    Yes!

    That can be done.. Can you attach a sample workbook?
    Cheers!
    Deep Dave

  3. #3
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi Gurus,

    I've attached a sample data in this post.

    Users need to update rows in B2 to D11, yellow highlighted.

    Once update is done, the row will be protected as explained previously.

    Thanks for your help.


    - Jack
    Attached Files Attached Files

  4. #4
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Lock and protect selected excel rows with VBA code

    So will the user fill in 1 row at a time and then that particular row is to be locked, or will the user fill in all the rows from 2 to 11, and then all the rows should be locked at once?

  5. #5
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi Gurus,

    User will need to update 1 row at a time, once done, that particular row will be locked.

    Thank you.


    - Jack

  6. #6
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Lock and protect selected excel rows with VBA code

    Hi,

    Please see the file attached.. The password to unlock the sheet is asd

    Let me know if it is as required.

    Do not forget to enable Macros..
    Attached Files Attached Files

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Lock and protect selected excel rows with VBA code

    Hi, SmallJack,

    Right-click on the worksheet-Tab, choose View Code and enter the following code into the code window:
    Please Login or Register  to view this content.
    Code will be triggered f any column form E on is entered or changed and will lock Columns B to E.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  8. #8
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi Holger,

    Thanks for your help.

    I've tried, but it locked all the rows, including those raws which have not been updated.

    I've attached my test data, I'm unable to update row 7 onwards.

    Please advice. Thanks.


    - Jack
    Attached Files Attached Files

  9. #9
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Lock and protect selected excel rows with VBA code

    Quote Originally Posted by SmallJack View Post
    Hi Holger,

    Thanks for your help.

    I've tried, but it locked all the rows, including those raws which have not been updated.

    I've attached my test data, I'm unable to update row 7 onwards.

    Please advice. Thanks.


    - Jack
    Hi,

    Did you happen to check out my solution?

  10. #10
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi Gurus,

    Thanks for the reply.

    I've tried your solution, but there are some issues.

    Users are only allowed to update the yellow highlighted area only.

    I've tried to lock outside yellow area with protection, but when I click on the button, it removes them.

    Also, I hit with an error when trying to update raw 7, see attached (SampleData-Test2.xls).

    The code should be able to cater the ongoing update within the yellow area.

    Appreciate for your help.

    Thank you.


    - Jack
    Attached Files Attached Files

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Lock and protect selected excel rows with VBA code

    Hi, Jack,

    jusrt unprotect the sheet again, run test and then use the altered Code behind the Sheet:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Besides: you should be more precise on what you really want. Iīm out of this thread.

    Ciao,
    Holger

  12. #12
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Lock and protect selected excel rows with VBA code

    Check it out now
    Attached Files Attached Files

  13. #13
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Lock and protect selected excel rows with VBA code

    Please Login or Register  to view this content.
    @NeedForExcel:
    Please enter Test into Cell B11 alone and run your code in your attachment. I doubt that all empty rows between 3 and 10 should be locked as well.

    Ciao,
    Holger

  14. #14
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Lock and protect selected excel rows with VBA code

    Quote Originally Posted by HaHoBe View Post
    Please Login or Register  to view this content.
    @NeedForExcel:
    Please enter Test into Cell B11 alone and run your code in your attachment. I doubt that all empty rows between 3 and 10 should be locked as well.

    Ciao,
    Holger
    Hi Holger,

    I have kept that in mind, however keeping in mind, normally data entry will happen one row after another, that's why like that..

  15. #15
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Lock and protect selected excel rows with VBA code

    Hi, NeedForExcel,

    no need for a full quote here - maybe have a look into the Forum Rules (#12).

    Any entry into Column B would have to leave Column C and D open for more entries in that row or how will you fill the entries in these columns when the row is locked?

    Ciao,
    Holger

  16. #16
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Lock and protect selected excel rows with VBA code

    Quote Originally Posted by HaHoBe View Post
    Any entry into Column B would have to leave Column C and D open for more entries in that row or how will you fill the entries in these columns when the row is locked?
    Agreed! But the user will lock the Row only when he finishes filling all the 4 Columns on that particular row wont he? Atlease that's what I think.

    Best if the OP states if he requires something otherwise.

  17. #17
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Lock and protect selected excel rows with VBA code

    Hi, NeedForExcel,

    the yellow highlighted area holds Columns B to D (which is 3 Columns) and the rows 2 to 11. With your code (which could be started at any time even if not entry or change was made) the whole area between B2 and F and the last entry row in Column B is blocked. I canīt agree on that from what I read that from post #10.

    Ciao,
    Holger

  18. #18
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi All,

    Thank you for your help and replies.

    Greatly appreciate it.



    - Jack

  19. #19
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi Gurus,

    How to incorporate solution given by Holger (post #13) into using a button to trigger the password lock?

    Thought it'll be more user friendly for users to manually lock the entry only when they've really finished entering the data.

    Thank you.



    - Jack

  20. #20
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Lock and protect selected excel rows with VBA code

    Hi, Jack,

    Thought it'll be more user friendly for users to manually lock the entry only when they've really finished entering the data.
    Sorry but to rely on extra action isnīt what I would call user-friendly.

    Please Login or Register  to view this content.
    Ciao,
    Holger

  21. #21
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi Holger,

    Great, works perfectly.

    Thanks for your help and replies.

    Have a nice day.
    Last edited by SmallJack; 04-13-2015 at 09:37 PM. Reason: update footer

  22. #22
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Lock and protect selected excel rows with VBA code

    Hi, Jack,

    maybe move the command to unprotect the sheet before the loop instead of using it inside there (itīs needed just once).

    Thanks for the fedback and the rep.

    Ciao,
    Holger

  23. #23
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi Holger,

    Thanks for the update.

    With the given code, but not using a button trigger method, can I check if it’s possible to protect multiple worksheets on opening or closing the excel file? For example of multiple worksheets: Sheet1, Sheet2, Sheet3 and soon.

    Appreciate for your help.

    Thank you.
    Last edited by SmallJack; 04-15-2015 at 02:30 AM. Reason: update information

  24. #24
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi Gurus,

    Anybody else can help out? Thanks.

  25. #25
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Lock and protect selected excel rows with VBA code

    Hi All,

    Never mind, closing the thread now.

    Thanks everyone

+ 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. [SOLVED] How lock rows and protect cells based on date in first column
    By udaybrown7 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-27-2014, 05:47 PM
  2. protect and lock rows
    By tkraju in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-15-2014, 11:46 PM
  3. [SOLVED] How to lock/protect a cell that is selected in a macro range
    By rwstarkey in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-20-2014, 08:09 PM
  4. Marco to Lock/Unlock and Protect/Unprotect Multiple Excel Workbooks
    By Jgonza25 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-22-2013, 03:29 PM
  5. MAcro to lock and unlock cells depending on rows selected.
    By ballack in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-25-2007, 11:04 PM

Tags for this Thread

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