+ 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

    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.

  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)

    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
    Hope that helps.

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

    Free DataBaseForm example

  3. #3
    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

  4. #4
    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

  5. #5
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

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

    rt1306,

    Please read forum rules below.

    I've wrapped you're code for you this time

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

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

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

    Great, it works well. Though there's still one issue. Can you set it so that it locks all the dates prior to today's date meaning it DOES NOT lock today's date. That would be greatly appreciated. Also on another note, my apologies on not wrapping the code.

    I want to thank royUK for generating the code and modytrane for tweaking it for me.

    rt1306

    Update: Nvm, I figured out how to make it less than today's date. Thank you all for the help. Problem is resolved
    Last edited by rt1306; 04-14-2009 at 02:11 PM. Reason: Additional info figured out how to do it

+ 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