entering a validated value into a cell

S

Sadcrab

I would like to know if I can prevent a certain value being entered into a
cell if the preceeding cell has acertain value. For example if cell 1A has
the value N entered in it can I prevent the value E being placed into cell
1B, obviously if any other value is in cell 1A I want to be able to enter the
value E. This is not a validating issue as I am also using validation rules
so that only certain values can be put into cells.
Thanks in advance
 
G

Gary''s Student

Try this tiny worksheet event macro. It is only an example for cells A1 and
B1:

Private Sub Worksheet_Change(ByVal Target As Range)
Set t = Target
Set a = Range("A1")
Set b = Range("B1")
If Intersect(t, b) Is Nothing Then Exit Sub
If a.Value <> "N" Then Exit Sub
If b.Value <> "E" Then Exit Sub
MsgBox ("E is not valid, try again")
Application.EnableEvents = False
b.ClearContents
b.Select
Application.EnableEvents = 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 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