Macro to check if current cell value is 1 and previous cell was 0
Give this a try. Right click the tab at the bottom of the worksheet you want
to have this functionality, select View Code from the popup menu that
appears and copy/paste the following into the code window that appeared
(change the MsgBox text argument to say what you want for the alert)...
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C42:M42")) Is Nothing Then
If (Target.Value = 1 Or Target.Value = 2) And _
Len(Target.Offset(0, -1).Value) 0 And _
Target.Offset(0, -1).Value = 0 Then
Target.Select
MsgBox "Put whatever alert message you want here"
End If
End If
End Sub
Note: This code only does what you asked, but I wonder, did you need an
alert message if there was a 1 or 2 in a cell (in the specified range) and
the user entered a 0 in the cell before it?
--
Rick (MVP - Excel)
"wissam" wrote in message
...
Hi.
I have a range of cells (C42:M42) where the user can enter values of
0,1,or
2 only (set up via data validation).
I need help with a code to do the following: if the worksheet user enters
a
value or changes the value in a cell in the above range (C42:M42), then
excel
would automatically check ( via a macro?) the value entered and compare
this
value to the value present in the previous cell on the same row. If the
value
entered is 1 or 2, and the value in the previous cell was 0, then it would
give a msgbox alert.
Any help is appreciated.
Thank you very much.
|