ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   entering a validated value into a cell (https://www.excelbanter.com/excel-programming/407215-entering-validated-value-into-cell.html)

Sadcrab

entering a validated value into a cell
 
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

Gary''s Student

entering a validated value into a cell
 
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

--
Gary''s Student - gsnu200771


"Sadcrab" wrote:

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



All times are GMT +1. The time now is 11:44 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com