Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Lock cells on the basis of date VBA

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13

    Lightbulb Lock cells on the basis of date VBA

    Hi, I am looking for a macro code to lock cell basis date. In the attached sheet i have employes who enters the data on daily basis. I wants to lock the cells once it is 2 days old from todays date, for example i wants t-he cells that are under Sep-1 to be locked after Sep 3rd (=today()-2) .
    Hope to have a solution from this fourmCell Lock.xlsx

    Thanks
    Rajesh

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi Rajesh,

    Please don't post question on Tips & Tricks forum. That forum meant to be provide cool tips and tricks only.
    If you have a specific question please post the question on the main forum.

    Paste the following code in ThisWorkbook module of your workbook.

    Code:
    Private Sub Workbook_Open()
        
        Dim wksTarget       As Worksheet
        Dim rngDate         As Range
        Dim rngData         As Range
        Dim c               As Long
        Dim LastRow         As Long
        Dim LastCol         As Long
        
        Const RangeToLock   As String = "C:AG" '<<  adjust to suit
        Const Pwd           As String = "pwd"
        
        Set wksTarget = ThisWorkbook.Worksheets("Sheet1") '<<  adjust to suit
        
        If Not blnUnlockedAllCells Then
            wksTarget.Cells.Locked = False
            blnUnlockedAllCells = True
            wksTarget.Protect Password:=Pwd, userinterfaceonly:=True
        End If
        
        With wksTarget
            LastRow = .Range("a" & .Rows.Count).End(xlUp).Row
            LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
            Set rngData = wksTarget.Range("a2", .Cells(LastRow, LastCol))
        End With
        
        For c = 3 To rngData.Columns.Count
            If CDate(rngData(1, c)) <= Date - 2 Then
                On Error Resume Next
                rngData.Columns(c).SpecialCells(2).Locked = True
                On Error GoTo 0
            End If
        Next
        
    End Sub
    adjust the sheet name and the range.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13
    Hi, I am looking for a macro code to lock cell basis date. In the attached sheet i have employes who enters the data on daily basis. I wants to lock the cells once it is 2 days old from todays date, for example i wants t-he cells that are under Sep-1 to be locked after Sep 3rd (=today()-2) .
    Hope to have a solution from this fourmCell Lock.xlsx

    Thanks
    Rajesh

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13
    My apology, I was not aware of this. Certainly I will keep this in mind. Should I reposting this question in the forum? also request you to please help me in setting range etc as I am in a learning phase of macro. Or it will be great if you can put the code in the same sheet and revert.

    Thanks
    Rajesh
    Attached Files Attached Files
    Last edited by Rajesh Kr Joshi; 09-08-2011 at 12:04 AM.

  5. #5
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi Rajesh,

    No need to apologise As I told you, you have to paste the above code in ThisWorkbook module of your workbook.

    Hit Alt + F11 > Double click on ThisWorkbook and paste the code there. I have commented the codes where you need to make adjustment. Like Sheet name, range, password etc.. I can't upload the workbook from office. I'll upload the workbook once I returned home.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  6. #6
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13
    Thank you. I will wait for the sheet , meanwhile i will also try your instruction

    Thanks
    Rajesh

  7. #7
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13
    Hi Thanks for the file, however when i am opening this file i am getting run time error 1004 and giving me an option to debug , when i clicked on dbug it is highlighting
    wksTarget.Cells.Locked = False in the code. sorry to bother you again and again. i wants the lock range C3:P12.

    Thanks
    Rajesh

  8. #8
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi Rajesh,

    PFA.
    Attached Files Attached Files
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  9. #9
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi Rajesh,

    Replace the existing code with the following. Save and close the workbook. Open the workbook again. (The code will run on opening the workbook)

    Code:
    Private Sub Workbook_Open()
        
        Dim wksTarget       As Worksheet
        Dim rngDate         As Range
        Dim rngData         As Range
        Dim c               As Long
        Dim LastRow         As Long
        Dim LastCol         As Long
        
        Const Pwd           As String = "pwd" '<<  adjust to suit
        
        Set wksTarget = ThisWorkbook.Worksheets("Sheet1") '<<  adjust to suit
        
        If Not blnUnlockedAllCells Then
            wksTarget.Protect Password:=Pwd, userinterfaceonly:=True
            wksTarget.Cells.Locked = False
            blnUnlockedAllCells = True
        End If
        
        Set rngData = wksTarget.Range("c2:p12") '<<  adjust to suit. range including the date row
        
        For c = 1 To rngData.Columns.Count
            If CDate(rngData(1, c)) <= Date - 2 Then
                On Error Resume Next
                rngData.Columns(c).SpecialCells(2).Locked = True
                On Error GoTo 0
            End If
        Next
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  10. #10
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13
    Thank you this worked great. One more question, what changes should i make if the date range in rows, not in colums.

    Thanks a ton
    Rajesh

Similar Threads

  1. Lock Cells After Data Entered
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 4
    Last Post: 06-28-2013, 10:52 PM
  2. Help- Locking column basis current date.
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 1
    Last Post: 03-25-2013, 04:44 PM
  3. automatic Add date in cells
    By Ryan_Bernal in forum Excel Help
    Replies: 1
    Last Post: 01-23-2013, 02:50 PM
  4. How to Lock or Unlock row basis previous cell input?
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 2
    Last Post: 07-25-2012, 02:40 PM
  5. Count no of cells containing date
    By princ_wns in forum Excel Help
    Replies: 5
    Last Post: 04-16-2012, 10:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •