View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 1,726
Default How to Run VBA Code on cell or range change

"MikeZz" wrote in message
...
Hi Bob,
I've never used Target/Intersect or even Worksheet_Change before and Excel
Help dialog doesn't always spell things out in layman's terms.



Worksheet_Change is one of the built-in events, which provide some extremely
useful application possibilities. Chip Pearson also noticed the help
deficiencies, and has written a good page on them at
http://www.cpearson.com/excel/events.htm


Can you explain how this line works? "(ByVal Target As Range)"

Is "Target" just a variable which is accepting the values in the range
that
is being changed? How does the routine know to send just the changed
values
or range?



Exactly that. Excel takes care of passing the changed range to the event
code, all you have to do is to test whether that range is part of the range
that you want to test.


and what does "Application.EnableEvents = False" does for me?
Based on your code, I could have an error and would exit the routine
before
EnableEvents is set back to True. Intuitvily this doesn't sound quite
right
but I've never used it so I'm just asking.



Switching EnableEvents off stops the event procedure calling itself over and
over if you change some other cells within the code.

If the code errors, it gets trapped by the line

On Error Goto ws_exit

and goes to that code label, which doesn't just exit, the first thing that
happens after that is to turn events back on, so order is restored.


I've been doing excel programing for years in my job and every time I
think
I've done just about everything, I find an entire new relm of
possibilities.



We're all in that boat <g.