Click to See Complete Forum and Search --> : Making Excel cells read-only using VBA


December 28th, 1999, 08:15 AM
Hai Code gurus,I got a problem. I am using VBA editor in Excel. I have written
a macro to get the data from ORACLE and print it on EXCEL. Now I want to make some cells read-only. Nobody should be allowed to change those particular cells. How can I do that?. Pls give your solution

With Regards

Ravi

Hannibal
December 28th, 1999, 09:44 AM
As far as I can tell (and I'm no guru) every cell in a worksheet is by default "locked". Now, this means that the contents cannot be changed. However, in order for the lock to work, you have to protect the sheet. So, assuming you want to only protect certain cells and give access to all the others, it would go like this:

--------------------<snip>------------------------

Sub CellLocker()
Cells.Select
' unlock all the cells
Selection.Locked = false
' next, select the cells (or range) that you want to make read only,
' here I used simply A1
Range("A1").Select
' lock those cells
Selection.Locked = true
' now we need to protect the sheet to restrict access to the cells.
' I protected only the contents you can add whatever you want
ActiveSheet.Protect DrawingObjects:=false, Contents:=true, Scenarios:=false
End Sub



--------------------</snip>-----------------------

once again, I'm no guru but I hope this helps. I'm not 100% sure, but I think you should be able to read values (when I lock them I know I can at least copy and paste values).

Hannibal
hannibal@vt.edu

December 28th, 1999, 09:53 AM
Dear Hannibal,

Thanks a lot for your suggestion. It's just fantastic and solved my problem.

Once again thanking you,

With Regards

Ravi

raviraj_manjule
February 17th, 2009, 06:05 AM
There is functionality is excel that we can stop users from copying the contents of cells, how can we implement the same using VB code. Kindly help