I'm populating a spreadsheet with Database values.The cells that I fill data with,I want them to be only read only to the users.How can I do this
In excel you can go to the Protection menu and specify which cells would require a password to modify. You can specify multiple ranges as well.
Hope this is what you were looking for.
You can try protecting cells. Or you can code it yourself using a SelectionChange handle event... :
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Selection, Range("A24:A50")) Is Nothing Then
Range("B1").Select
End If
End Sub
Depends on how you want to select the range. This is just one cell. If you go back to J1 and change the value, you should get prompted.
Private Sub Worksheet_Change(ByVal Target As Range)
Range("J1").Select
Selection.Locked = True
ActiveSheet.Protect Contents:=True
Range("K1").Select
End Sub
Cells are not locked until the worksheet is protected. By default all cells are set to Locked, so you'll have to unlock cells you want users to be able to change.
In these circumstances I often find the best way is to lock the sheet, but only for the user by using the UserInterfaceOnly
argument which still allows unrestricted programmatic interaction with the sheet.
ActiveSheet.ProtectUser InterfaceOnly:=True
There are various other arguments that can be set that will still allow the user to filter, sort etc. should this be required, the Help file has a full list.