View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
[email protected] scotty01@aol.com is offline
external usenet poster
 
Posts: 2
Default worksheet_calculate

I copied this simple code below from this site recently, to learn more
about vba code. It is my understanding that using worksheet_calculate,
the code should execute automatically upon changes to the worksheet.
But when I change the value in A2, the value in D2 does not change
unless I go to the code in the VBA editor and click on run sub/
userform (or press the F5 key).

I am working in Sheet1, and have the code in Sheet1, so I don't
understand why the code does not execute automatically. Automatic
calculation is set to 'on' (tools/options). I have tried other
worksheet_calculate examples with the same result (no automatic
execution) and I have tried running the program from a different pc
(just to see if I may have inadvertently set some option). I have also
exited the spreadsheet and re-opened, in case enableevents had not
been reset to 'true'.

There is probably a simple explanation, but it's all new to me.

Thanks for any help.

Scott

Private Sub Worksheet_Calculate()
'Must disable events otherwise will run again
'when A2 is saved to D2
Application.EnableEvents = False


If Range("A2") < Range("D2") Then 'Value has changed
Range("d2") = Range("A2") 'Resave new A2 value
End If


Application.EnableEvents = True
End Sub