The selection change event will not reference the cell you've just left but the change event will, assuming a change has been made to that cell. So if you are wanting to validate the cell in some way, then use the change event.
such as:
Private Sub Worksheet_Change(ByVal Target As Range)
if Target.Address = "$B$4" then
if target.value <> 42 then
msgbox "That is not the answer"
end if
end if
End Sub
Target.Column and row etc allows you, well you get the drift.
Of course, Data/Validation would do most validation tasks anyway, that's just an example.
Thanks for your responses, I must be being really thick.
When I try and paste some of the sample code you've provided into my worksheet I can't get it to run at all - the macro name doesn't appear in the list of available macros within the worksheet, can someone suggest what I am doing wrong.
You need to put the event handlers on the code for your worksheet or your workbook, not in a normal code module. In the VB editor, right-click on the worksheet you're trying to work with (in the project explorer window in the left of your screen), and select "view code" - this will display in the main window the code module for the worksheet. Put the code from the suggestions above here. Rob
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.