+ Reply to Thread
Results 1 to 23 of 23

Lock cells after Today's date passed (VBA excel code)

Hybrid View

  1. #1
    Registered User
    Join Date
    04-09-2009
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel, Access, Word, PowerPoint 2003
    Posts
    27

    Lock cells after Today's date passed (VBA excel code)

    I am new to this forum so hello to all.

    I am trying to lock cells after today's date has passed so that no one can make changes to it after today's date has lapsed. This is for protective reasons so that people do not remove their names from reserving something after using it. Now the code should disallow locking after cell input entry when today's date hasn't passed so that changes can still be made by the user. I am trying to determine the code to do this but I have no idea as to how to do it.

    Here's a scenario: I reserve something for Aprill 11, 2009. I input my name. Since it's April 9th, 2009, I am still able to make changes up and until April 11, 2009. After this date, the cell is locked and no changes can be made, except for the administrator.

    Also, a code to allow the administrator to make changes within the cells if need be. That would be appreciated.

    Please advise,

    Thank you
    Last edited by rt1306; 04-14-2009 at 02:17 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Lock cells after Today's date passed (VBA excel code)

    Will here be other cells containing reservations or just the one?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Lock cells after Today's date passed (VBA excel code)

    You need to lock the sheets and only unprotect them in the Open event. Otherwise, they can disable macros and your code would not work.

    See this for an example that relates a bit. http://www.vbaexpress.com/forum/showthread.php?t=22918

  4. #4
    Registered User
    Join Date
    04-09-2009
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel, Access, Word, PowerPoint 2003
    Posts
    27

    Re: Lock cells after Today's date passed (VBA excel code)

    Well, there will be cells that contain a reservation name before and after today's date. I want to automatically lock all cells from (D7:J7675) prior to today's date (not including today's date) so that no changes can be made unless you have administrative privileges. This is to protect the integrity of the reservation so that when the reserved thing has been used, the user cannot remove their name to illustrate that they did not use it.

    I've attached a sample reservation schedule for reference as to what I mean.

    Please Advise,

    Thank you
    Attached Files Attached Files
    Last edited by rt1306; 04-09-2009 at 10:55 AM.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Lock cells after Today's date passed (VBA excel code)

    using merged cells makes this more complicated. Try this code in the WorkBook_Open event. Note you must first of all unlock all cells in that range - Format -> Cells -> Protection and de-select Locked

    Option Explicit
    
    Private Sub Workbook_Open()
        Const PW   As String = "secret"
        Dim rBookings As Range
        Dim cl     As Range
        Dim mArea  As Range
        With Sheets("Availability")
            .Unprotect PW
            Set rBookings = .Range(.Cells(7, 4), .Cells(7675, 10)).SpecialCells(xlCellTypeConstants)
            For Each cl In rBookings
                Set mArea = .Cells(cl.Row, 1).MergeArea
                If mArea.Cells(1, 1).Value <= Date Then cl.Locked = True
            Next cl
            .Protect PW
        End With
    End Sub
    Copy the code
    Select the workbook in which you want to use the code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    In the Project Explorer, find your workbook, and open the list of Microsoft Excel Objects
    Right-click on the ThisWorkbook object, and choose View Code
    Where the cursor is flashing, choose Edit | Paste

  6. #6
    Registered User
    Join Date
    04-09-2009
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel, Access, Word, PowerPoint 2003
    Posts
    27

    Re: Lock cells after Today's date passed (VBA excel code)

    Hi,

    Thank you for the code though there seems to be a run time error '1004

    Option Explicit
    
    Private Sub Workbook_Open()
        Const PW   As String = "secret"
        Dim rBookings As Range
        Dim cl     As Range
        Dim mArea  As Range
        With Sheets("Availability")
            .Unprotect PW
    Set rBookings = .Range(.Cells(7, 4), .Cells(7675, 10)).SpecialCells(xlCellTypeConstants)
    For Each cl In rBookings
                Set mArea = .Cells(cl.Row, 1).MergeArea
                If mArea.Cells(1, 1).Value <= Date Then cl.Locked = True
            Next cl
            .Protect PW
        End With
    End Sub
    Thank you for the help. Please advise on this error.
    Last edited by VBA Noob; 04-13-2009 at 01:16 PM. Reason: Added code tags as per forum rules

  7. #7
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Lock cells after Today's date passed (VBA excel code)

    I tried Roy's code with one small change and it seems to work.
    see attached file.
    modytrane
    Attached Files Attached Files

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Lock cells after Today's date passed (VBA excel code)

    Your merged cells containing the dates make the task harder.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Lock cells after Today's date passed (VBA excel code)

    Change <= to <

    The only reason that I can see for the error is if no cells in the range have values. I used Specialcells to reduce the range to check. Try this amended code
    Option Explicit
    
    Private Sub Workbook_Open()
        Const PW   As String = "secret"
        Dim rBookings As Range
        Dim cl     As Range
        Dim mArea  As Range
        With Sheets("Availability")
            .Unprotect PW
    'if no cells have entries in the range the code will error
    'the exit handler skips to the point where the password is reset
            On Error GoTo exithandler
            Set rBookings = .Range(.Cells(7, 4), .Cells(7675, 10)).SpecialCells(xlCellTypeConstants)
            MsgBox rBookings.Address
            For Each cl In rBookings
                Set mArea = .Cells(cl.Row, 1).MergeArea
                If mArea.Cells(1, 1).Value < Date Then cl.Locked = True
            Next cl
    exithandler:
            .Protect PW
        End With
    End Sub
    Last edited by royUK; 04-14-2009 at 02:21 PM.

  10. #10
    Registered User
    Join Date
    04-09-2009
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel, Access, Word, PowerPoint 2003
    Posts
    27

    Re: Lock cells after Today's date passed (VBA excel code)

    Hi RoyUK, how do I mark it as solved?

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Lock cells after Today's date passed (VBA excel code)

    Quote Originally Posted by rt1306 View Post
    Hi RoyUK, how do I mark it as solved?
    You cannot if the first post was more than two days ago. I'll do it for you.

    Did you try the amended code above?

  12. #12
    Registered User
    Join Date
    04-09-2009
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel, Access, Word, PowerPoint 2003
    Posts
    27

    Re: Lock cells after Today's date passed (VBA excel code)

    I did amend the code. Thank you RoyUK.

  13. #13
    Registered User
    Join Date
    04-09-2009
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel, Access, Word, PowerPoint 2003
    Posts
    27

    Re: Lock cells after Today's date passed (VBA excel code)

    Help
    The code didn't lock yesterday's date. It's still unlocked...All other cells from April 13 and prior are locked but April 14 is not. Can someone help make changes?

    I've attached a new spreadsheet with the current code in it.

    Thank you
    Attached Files Attached Files
    Last edited by rt1306; 04-15-2009 at 10:05 AM.

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Lock cells after Today's date passed (VBA excel code)

    If you want to lock all the cells then remove the specialcells
    Option Explicit
    
    Private Sub Workbook_Open()
        Const PW   As String = "secret"
        Dim rBookings As Range
        Dim cl     As Range
        Dim mArea  As Range
        With Sheets("Availability")
            .Unprotect PW
            'if no cells have entries in the range the code will error
            'the exit handler skips to the point where the password is reset
            On Error GoTo exithandler
            Set rBookings = .Range(.Cells(7, 4), .Cells(7675, 10))
            For Each cl In rBookings
                Set mArea = .Cells(cl.Row, 1).MergeArea
                If mArea.Cells(1, 1).Value < Date Then cl.Locked = True
            Next cl
    exithandler:
            .Protect PW
        End With
    End Sub

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Lock cells after Today's date passed (VBA excel code)

    The sheet needs to be protected to allow cells to be Locked. As a result user actions are restricted.

    To edit locked cells simply use the password to unprotect the sheet

  16. #16
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Lock cells after Today's date passed (VBA excel code)

    Set your computer date by the calendar in the system tray to test.

  17. #17
    Registered User
    Join Date
    04-09-2009
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel, Access, Word, PowerPoint 2003
    Posts
    27

    Re: Lock cells after Today's date passed (VBA excel code)

    Can there not be an extra code to enable all functions from today's date and later?

    Btw, thanks for the suggestion Kenneth. Nice mini test. Though it still didn't lock all the cells prior to today's date.
    Last edited by rt1306; 04-16-2009 at 11:20 AM.

  18. #18
    Forum Contributor
    Join Date
    05-01-2013
    Location
    Canada
    MS-Off Ver
    Office 365
    Posts
    165

    Re: Lock cells after Today's date passed (VBA excel code)

    How I can set VBA to my active worksheet only print if Cell L41 have today date, I have 31 sheet in my workbook so only active work sheet print if date is correct.

    Thanks

  19. #19
    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,198

    Re: Lock cells after Today's date passed (VBA excel code)

    Hi, robrobet,

    Welcome to the Forum.

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

    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

  20. #20
    Forum Contributor
    Join Date
    05-01-2013
    Location
    Canada
    MS-Off Ver
    Office 365
    Posts
    165

    Re: Lock cells after Today's date passed (VBA excel code)

    yes I got it , Thanks to correct me .

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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