Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: Lock cells on the basis of date VBA

  1. #11
    Grand Master
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    10
    Code:
        Dim wksTarget           As Worksheet
        Dim rngDate             As Range
        Dim rngData             As Range
        Dim c                   As Long
        Dim LastRow             As Long
        Dim LastCol             As Long
        Dim blnUnlockedAllCells As Boolean
        
        Const Pwd               As String = "pwd" '<<  adjust to suit
        
        Set wksTarget = ThisWorkbook.Worksheets("Sheet2") '<<  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("B3:L16") '<<  adjust to suit. range including the date row
        
        For c = 1 To rngData.Rows.Count
            If CDate(rngData(c, 1)) <= Date - 2 Then
                On Error Resume Next
                rngData.Rows(c).SpecialCells(2).Locked = True
                On Error GoTo 0
            End If
        Next
    Attached Files Attached Files
    Last edited by S M C; 09-11-2011 at 07:49 PM.

  2. #12
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13
    Thank a lot this is working awesome.Need one more help or rather I should have ask this question in the earlier post- Currently this code is locking only those cells which have data in it (or written something), is it possible to lock all the cell in the given range.

    Thnaks
    Rajesh

  3. #13
    Grand Master
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    10
    Just remove
    Code:
    .Specialcells(2)

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

    Since your post counts exceed more than 10, you are now able to download the Classic 2003 Excel Menu from here
    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)

  5. #15
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13
    Hi ,
    Experts, Your solution is working fine, I need one more help from you. Attached is the file in which I wants to implement the macro, but I stuck at a point wherein apart from the green cells, by default all the other cells should be locked, because the green cells are the date base cell lock range. But with the current code only those cells are locked which are with the date range and all other cells are unlocked. It will be great If you can add a tweak in the existing code to fulfil the above requirement. And this macro should start automatically with the sheet open. My miss i should have given all the requirement one go ..thanks for all your assistance.

    Thanks
    Rajesh
    Attached Files Attached Files

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

    You mean like this

    Code:
    Option Explicit
    Dim blnUnlockedAllCells As Boolean
    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
        Dim blnUnlockedAllCells As Boolean
        
        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("$C$6:$Y$381") '<<  adjust to suit. range including the date Column
        
        For c = 1 To rngData.Rows.Count
            If CDate(rngData(c, 1)) <= Date - 2 Then
                On Error Resume Next
                rngData.Rows(c).Locked = True
                On Error GoTo 0
            End If
        Next
        
    End Sub
    Paste this code in 'ThisWorkbook' module.
    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)

  7. #17
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13
    Hi, It is working same as the same previous code. The date base cells are getting locked, but all the other cells are still unlocked. To make is easy to understand , i have color coded the cells in the attached sheet. I wants all the gray cells to be locked by default when the sheet opens and the green cells will be locked on date basis.

    Thanks
    Rajesh
    Attached Files Attached Files

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

    Try this one.

    Code:
    Option Explicit
    Dim blnUnlockedAllCells As Boolean
    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
        Dim blnUnlockedAllCells As Boolean
        
        Const Pwd               As String = "pwd" '<<  adjust to suit
        
        Set wksTarget = ThisWorkbook.Worksheets("Sheet1") '<<  adjust to suit
        Set rngData = wksTarget.Range("$C$6:$Y$381") '<<  adjust to suit. range including the date Column
        
        If Not blnUnlockedAllCells Then
            wksTarget.Cells.Locked = True
            rngData.Locked = False
            wksTarget.Protect Password:=Pwd, userinterfaceonly:=True
            blnUnlockedAllCells = True
        End If
        
        For c = 1 To rngData.Rows.Count
            If CDate(rngData(c, 1)) <= Date - 2 Then
                On Error Resume Next
                rngData.Rows(c).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)

  9. #19
    Member
    Join Date
    Aug 2011
    Posts
    92
    Rep Power
    13
    Hi, This works great....Thanks a ton for all your help.

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

    You are welcome
    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)

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
  •