I read a lot of pages saying that, but none of them put the solution if the value change by an "if function" not by hand.
The code I get is that:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A18:A30")) Is Nothing Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
On Error GoTo Finalize 'to re-enable the events
MsgBox "You changed THE CELL!"
Finalize:
Application.EnableEvents = True
End Sub
It only works if I change the value by hand.
Thank you in advance.
Another solution; instead of triggering your function every time when your worksheet recalculates, add a function in a module:
Assuming the outcome of your formula is numeric:(otherwise outcome of function must be a empty string and the "+" must be "&") Add to your IF-formula at the end ...+Detectchange() Now there will be a msgbox only when your formula is recalculated
Edit by Darren Bartrup-Cook:
I found this code gave worked when the formula recalculated. It didn't fire if I changed a cell that doesn't affect the cell it's entered to and it didn't fire using
Calculate Now
orCalculate Sheet
.It did occasionally fire for all formula that I used the function in, but that seemed to be when I was debugging - maybe further investigation needed.
e.g.:
=IF(A1=1,A2,A3) & DetectChange()
entered in cell A4 displays the message "You changed cell $A$4" if cells A1, A2 or A3 is changed.Write this in
Sheet1
and run theTestMe
sub:It has worked quite ok on my PC.
If the cell is changed by a built-in Excel function, then the comment of @Vincent G states the correct answer:
Worksheet_Change event occurs when cells on the worksheet are changed by the user or by an external link. and This event does not occur when cells change during a recalculation. Use the Calculate event to trap a sheet recalculation.
If you want to track the calclulation event based on some changes at
Range(A18:A30)
this is a working solution:Sheet2
);In the current Worksheet write the Calculate event:
In the
Sheet2
write an event, catching the changes.As simple as @Vincent G says.