Lock the particular cells after date is over

RAMU

Active Member
Joined
Dec 11, 2009
Messages
345
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
Hi Friends,

Hv a workbook with multiple sheets (months).

In july sheet I need these feature:

July

ABCDEFGHIJK
START FINISH
DATETIME DATETIME HOUR QTY

<colgroup><col style="WIDTH: 30px; FONT-WEIGHT: bold"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"></colgroup><tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]

[TD="bgcolor: #cacaca, align: center"]3[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]

[TD="align: right"]25-07-17[/TD]
[TD="align: right"]10:30[/TD]

[TD="align: right"]25-07-17[/TD]
[TD="align: right"]18:15[/TD]

[TD="align: right"]7:45[/TD]

[TD="bgcolor: #99ccff, align: right"]30[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]

[TD="bgcolor: #ffff00"] [/TD]

[TD="bgcolor: #ffff00"] [/TD]

[TD="bgcolor: #99ccff"] [/TD]

[TD="bgcolor: #cacaca, align: center"]6[/TD]

[TD="bgcolor: #99ccff"] [/TD]

[TD="bgcolor: #cacaca, align: center"]7[/TD]

[TD="bgcolor: #99ccff"] [/TD]

[TD="bgcolor: #cacaca, align: center"]8[/TD]

[TD="bgcolor: #99ccff"] [/TD]

[TD="bgcolor: #cacaca, align: center"]9[/TD]

[TD="bgcolor: #99ccff"] [/TD]

[TD="bgcolor: #cacaca, align: center"]10[/TD]

[TD="bgcolor: #99ccff"] [/TD]

[TD="bgcolor: #cacaca, align: center"]11[/TD]

[TD="bgcolor: #99ccff"] [/TD]

</tbody>

Spreadsheet Formulas
CellFormula
B4=TODAY()
F4=TODAY()
I4=+G4-C4

<tbody>
</tbody>

<tbody>
</tbody>

In Row 4 Today's data will be entered. Tomorrow or any day after today when the workbook will be opened Row 4 will become locked automatically. only admin will be able to unlock that.

At the same time B5 & F5 (yellow highlighted) will show the date, on the day the workbook will be opened but if I open today again or the same day it will not show. Then someone will enter the data on that day. when the day will over, again the Row 5 will be locked automatically, that means after 26th or any day the row 4 & 5 will be locked, rest will remain as unlocked.

pls help me to provide a solution.

Regards
RAMU
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I may have a solution for this but I must wait until tomorrow to be sure.
Anyway I'll offer the following code with one important note: B1 must have something in it like "Record"/"Inventory" or whatever.
You must also Highlight the whole sheet, right click and select Format cells, select Protect and uncheck the locked option.
Add the code to VBA and then select Review/Protect Sheet. Then save and close the file.

If my advice is correct auto_lock is activated, will lock the prior date row and set the sheet up for whatever "TODAY" happens to be.

Code:
Sub auto_lock()
'
' LockMe Macro
'

'
 lastrow = Cells(Rows.Count, "B").End(xlUp).Row
    Rows("4:4").Select
    Selection.Locked = True
    Selection.FormulaHidden = False
    Range("B" & lastrow).Formula = "=Today()"
    Range("F" & lastrow).Formula = "=Today()"
    Range("H" & lastrow).Value = Range("G" & lastrow).Value - Range("C" & lastrow).Value
    
   
End Sub
 
Upvote 0
I may have a solution for this but I must wait until tomorrow to be sure.
Anyway I'll offer the following code with one important note: B1 must have something in it like "Record"/"Inventory" or whatever.
You must also Highlight the whole sheet, right click and select Format cells, select Protect and uncheck the locked option.
Add the code to VBA and then select Review/Protect Sheet. Then save and close the file.

If my advice is correct auto_lock is activated, will lock the prior date row and set the sheet up for whatever "TODAY" happens to be.

Code:
Sub auto_lock()
'
' LockMe Macro
'

'
 lastrow = Cells(Rows.Count, "B").End(xlUp).Row
    Rows("4:4").Select
    Selection.Locked = True
    Selection.FormulaHidden = False
    Range("B" & lastrow).Formula = "=Today()"
    Range("F" & lastrow).Formula = "=Today()"
    Range("H" & lastrow).Value = Range("G" & lastrow).Value - Range("C" & lastrow).Value
    
   
End Sub

Hi,

I tried as per your instruction but not a single cell is locked. May be my process was wrong.
 
Upvote 0
Hours after I posted I realised that the =TODAY formula is not updated. That value should be converted to a specific value.
Then I should have run a test to ask if the date in the last column is TODAY. If it is not then the macro would lock the last line before adding the new line.

I shall relook at that but I'm still dubious if Excel does recognise that auto_lock() is meant to run automatically upon opening.
My apologies for my oversights.
 
Upvote 0
I am about to leave home and will not return until later in the day but I have something that I believe now works.
I'll explain then.
 
Upvote 0
its ok, i will do that.
Hours after I posted I realised that the =TODAY formula is not updated. That value should be converted to a specific value.
Then I should have run a test to ask if the date in the last column is TODAY. If it is not then the macro would lock the last line before adding the new line.

I shall relook at that but I'm still dubious if Excel does recognise that auto_lock() is meant to run automatically upon opening.
My apologies for my oversights.
 
Upvote 0
I'm not sure if this is shared. I hope so. In any case the file is set up exactly as I mentioned above except that I put =TODAY in A1
The macro needs to see that. The macro also uses the Review>Protect/Unprotect features automatically.


This is the altered code:
Code:
Sub auto_lock()
'
' LockMe Macro
'

'
 ActiveSheet.Unprotect
 lastrow = Cells(Rows.Count, "B").End(xlUp).Row
    
    'Selection.FormulaHidden = False
    If Range("B" & lastrow).Value <> Range("A1").Value Then
    Rows(lastrow & ":" & lastrow).Select
    Selection.Locked = True
    Range("B" & lastrow + 1).Formula = "=Today()"
    Range("F" & lastrow + 1).Formula = "=Today()"
    Range("B" & lastrow + 1).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("F" & lastrow + 1).Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End If
   
End Sub

Sub overwrite()
'
' overwrite Macro
'

'
    Range("B4").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("F4").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
Tell me if you have issues.

Dropbox - LockCells.xlsm



 
Last edited:
Upvote 0
The macro works as required except .... it does NOT autorun when the file is opened; that has been my concern from the start.
I presume that you are aware that you can add a password using the Review>Protect option.

It may be necessary to attach the macro to a button but that would still leave "yesterday's" data vulnerable until the macro is run.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top