Hi
Please always give as much information as possible about where you get macros from, and how it came to be. ( This is often very helpful to people trying to help with modifications to the program. If you are getting help from different places and do not tell us, then it can lead to people duplicating work for you unnecessarily. )
So the second macro is fine, Private Sub Worksheet_selectionChange(ByVal Target As Range)
You will see that it is triggered by
Code:
Set Rg = Application.Intersect(Target, Range("Z20"))
If Not Rg Is Nothing Then
What is happening there is that only
If the selected range , ( Target ) , and Z20 coincide ( cross / intersect)
will anything be done
That is fine for triggering when you selected Z20
In the first macro, which doesn’t currently work as you want it to, you are currently trying to trigger using exactly the same code lines. So that is no good. It can’t work if you selected a cell other than Z20
If I understand correctly, the cell Z20 may change as a result of you … whenever a value is entered in the cell "P20" or "U20", and P20<U20 (as the formula set in cell Z20), immediately the msgbox should appear….
So you are selecting P20 or U20 ,
I think it is best to forget the first macro as you have it that you currently have, and start again.
A working macro will be very simple and similar to what we have done together now a few times…
Remember, Target , is the range that you selected. ( Excel fills the variable, Target , with the range object that you selected )
That is probably the most imortant thing to remember in such coding. Allmost all of this sort of coding will use Target in one or more ways. Knowing about the Properties of the range object that is Target goes a long way to tell us what happened, and at what state things currently are.
Code:
Private Sub Worksheet_Change(ByVal Target As Range) ' https://excelfox.com/forum/showthread.php/2627-Conditional-Appearance-of-msgbox?p=14859#post14859
If Target.Address = "$P$20" Or Target.Address = "$U$20" Then
If Range("Z20").Value = "??" Then MsgBox Prompt:="Entry Error"
Else
' a cell or cells other than P20 Or U20 where selected, so nothing is done
End If
End Sub
Alan
Bookmarks