VBA: How to lock cells after data entry

lennarp

New Member
Joined
Jan 17, 2013
Messages
2
Hi All,

I am having some trouble with achieving my desired outcome in Excel.

I am setting up a spreadsheet that gives the ability of the user to enter timmings for specific activities. I am using a timestamp whereby the user can click on column 2 & column 6
which is the 'Start' & 'End' time. In column 7 would give the total time it took to complete that activity/phone call (= column 6 - column 2).

Here is my initial code which appears to work well and gives the time down to the second.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)</SPAN></SPAN>
If Target.Columns.Count > 1 Then Exit Sub</SPAN></SPAN>
If Target.Column <> 6 And Target.Column <> 2 Then Exit Sub</SPAN></SPAN>
Target.Value = Now - Date</SPAN></SPAN>
Target.NumberFormat = "h:m:ss"</SPAN></SPAN>

End Sub


However I need to ensure that when users enter data into both these columns the cells automatically lock to ensure they dont overwrite the data. Furthermore if the user clicks on the entire Column by selecting 'column B' for instances it automatically populates the data in every single cell from B1-B1000000 ETC.


1. How can I look the cells so it does not overwrite previous data
2. Ensure that if users click on the entire column it wont pre populate thounds of cells of data.


Any help would be appreciated.

Cheers
</SPAN></SPAN>
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,261
This won't overwrite a time stamp if they select a previously populated cell, but it doesn't "lock" the data either. Is that what you want?

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] Worksheet_SelectionChange([color=darkblue]ByVal[/color] Target [color=darkblue]As[/color] Range)
    [color=darkblue]If[/color] Target.Count > 1 [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    [color=darkblue]If[/color] Target.Column <> 6 And Target.Column <> 2 [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    [color=darkblue]If[/color] Target.Value <> "" [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    Target.Value = Time
    Target.NumberFormat = "h:m:ss"
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 

Watch MrExcel Video

Forum statistics

Threads
1,095,750
Messages
5,446,271
Members
405,393
Latest member
sully361

This Week's Hot Topics

Top