+ Reply to Thread
Results 1 to 10 of 10

locking cells automatically

  1. #1
    Forum Contributor
    Join Date
    04-29-2012
    Location
    nuneaton, England
    MS-Off Ver
    Office 365
    Posts
    372

    locking cells automatically

    could someone tell me how to lock cells automatically in excel.

    i have a date field in column a and i want the code to look at the cells in column a and lock the whole row if that cell is greater that 1 week old.

    i dont use a password in the protect sheet but i just want to limit accidental deletions or alterations.


    thanks

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: locking cells automatically

    If you lock a cell without protecting the sheet then it won't do anything, although you can protect the sheet without a password.

    You might be able to achieve this with some sort of data validation if you don't protect the sheet.

  3. #3
    Forum Contributor
    Join Date
    04-29-2012
    Location
    nuneaton, England
    MS-Off Ver
    Office 365
    Posts
    372

    Re: locking cells automatically

    i use the protect sheet fuction within Excel, but i dont put any text in. But if code needs a pwsd then i will use one. like i said, its mainly to stop people from changing data accidently as they will get a prompt saying that the cells are protected.

    i just want the cells to lock automatically if they are over a week old. i.e the sheet is protected, but only rows where the date in a is greater than 1 week... something like

    PHP Code: 
    Private Sub Worksheet_Calculate()
    If 
    Range("A:A").Value <today() Then
        Me
    .protect Password:=""
        
    Range("A:A").Locked False
        Me
    .Protect Password:=""
    End Sub 
    (i know this code wont work but its an example, i want it to lock the whole row)
    Last edited by wayneg; 12-17-2013 at 06:21 AM.

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: locking cells automatically

    Well the code would be something like:
    Please Login or Register  to view this content.
    You would need to assign it to an event, it could be when you open the workbook, when you change a cell etc. I would suggest that running it everytime the workbook opens is probably ok? For that you need:
    Please Login or Register  to view this content.
    saved in the "thisworkbook" object module.

  5. #5
    Forum Contributor
    Join Date
    04-29-2012
    Location
    nuneaton, England
    MS-Off Ver
    Office 365
    Posts
    372

    Re: locking cells automatically

    i cant seem to get that to work. could you upload a sample please

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: locking cells automatically

    Hi,

    Sorry uploads are not working for me at the moment, I had a look and made some changes to the code:
    Please Login or Register  to view this content.
    Change the sheetname as neccessary.

  7. #7
    Forum Contributor
    Join Date
    04-29-2012
    Location
    nuneaton, England
    MS-Off Ver
    Office 365
    Posts
    372

    Re: locking cells automatically

    where am i adding this as i tried on sheet1 , tried on thisworkbook and tried as a module and then saved it, but i can still edit the rows where there is a date older that today. sorry to be a pain in the ..... lol

  8. #8
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: locking cells automatically

    thisworkbook.

    It will run when the workbook opens - you can press f5/click run in the macro manually to test though. Maybe if you try uploading your workbook with the macro as you are trying to use it I can have a look.

  9. #9
    Forum Contributor
    Join Date
    04-29-2012
    Location
    nuneaton, England
    MS-Off Ver
    Office 365
    Posts
    372

    Re: locking cells automatically

    i think i have managed to do it now. thank you

  10. #10
    Forum Contributor
    Join Date
    04-29-2012
    Location
    nuneaton, England
    MS-Off Ver
    Office 365
    Posts
    372

    Re: locking cells automatically

    ive realised that after i marked this as solved it doesnt work on the values that is part of a table. it works on the values that isnt in a table so is there anything different i need to do.

+ 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. Locking specific cells eventually changing to locking all cells
    By davidingilbert in forum Excel General
    Replies: 1
    Last Post: 03-19-2013, 06:50 PM
  2. Replies: 2
    Last Post: 01-24-2013, 07:59 AM
  3. Automatically Locking VBA Project Code
    By Eric Excels in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-30-2011, 07:17 PM
  4. Help with macro, automatical locking cells when excel is closed but only locking 1 sh
    By snoopy1461 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-21-2011, 11:29 AM
  5. Replies: 1
    Last Post: 07-21-2006, 11:05 AM

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