Unlock a cell in vba
Solved
Anthelm
Posted messages
202
Status
Member
-
Anthelm Posted messages 202 Status Member -
Anthelm Posted messages 202 Status Member -
Bonjour,
I have a locked sheet containing a cell with a "Date" function. I want to create a macro that freezes this date by replacing it with today's date (so if I open the file the next day, the date hasn't changed)
ActiveWorkbook.Sheets("Impression").Copy
Cells("G1").Locked = False
Range("G1") = Date
The second line is causing an error. I've tried a million solutions...
The problem is that before the macro, G1 is locked, and it should be unlocked after using the macro (which first copies the document)
Thank you very much!
I have a locked sheet containing a cell with a "Date" function. I want to create a macro that freezes this date by replacing it with today's date (so if I open the file the next day, the date hasn't changed)
ActiveWorkbook.Sheets("Impression").Copy
Cells("G1").Locked = False
Range("G1") = Date
The second line is causing an error. I've tried a million solutions...
The problem is that before the macro, G1 is locked, and it should be unlocked after using the macro (which first copies the document)
Thank you very much!
2 answers
-
Hello Anthelm,
Two things
- with the "cells" function, we code the address of the cell numerically in the form of (row address, column address) meaning that to select B1 with cells, we code cells(1,2)
whereas if you use range, you code: range("B1")
- The "locked" function determines whether the cell in question will be protected or not once the sheet is protected.
To be able to modify a cell in a macro whether it is "locked = true" or "locked = false," you just need to unprotect the sheet at the beginning of the procedure and re-protect the sheet at the end of the procedure.
Here is an example of code:ActiveSheet.Unprotect Range("G1") = Date ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
To test
Jc -
Good evening
try with these two lines in your macro:
Range("G1").Locked = False
Range("G1").Value = Date
although for the 1st line, it doesn’t seem necessary to unlock the cell if the sheet is not protected.
And you won’t be able to unlock it without removing the protection
either at the beginning of the macro:
activesheet.unprotect
and at the end
activesheet.protect
best regards