Lock after entering

B

bgkgmg

Cell B2 has the formula SUM(A1,A3). After entering data in A1 and A3 the sum
appears in B2. I would like to lock only B2 after data entry so it never
changes. Is it possible?

Thanks
 
G

Gary''s Student

Start with all cells ujnlocked and the sheet unprotected.

The following worksheet event macro will monitor A1 and A3. Once they are
both filled in, the formula in B2 will be converted to a value and B2 will be
locked:

Private Sub Worksheet_Calculate()
Set a1 = Range("A1")
Set a3 = Range("A3")
Set b2 = Range("B2")
If IsEmpty(a1.Value) Then Exit Sub
If IsEmpty(a3.Value) Then Exit Sub
If Not b2.HasFormula Then Exit Sub
b2.Value = b2.Value
b2.Locked = True
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub


Because it is worksheet code, it is very easy to install and automatic to use:

1. right-click the tab name near the bottom of the Excel window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it.


To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top