So, if our cursor is in cell A1 and we press enter, the Target variable will refer to cell A1. The Target variable refers to the cell we edit. So, if Target refers to cell A1, we can not use it to directly refer to cell A2 (we can however refer to it indirectly). The ByVal keyword specifies that the underlying variable that Target represents cannot be altered by our stored procedure. For example, the Worksheet_Change event passes along the ByVal Target as Range variable. Most events also pass along information to their subscribers. It’s important that you don’t change the name of your subroutine as this is what is being triggered by the event. Once you select an event to subscribe to, the editor will automatically generate the necessary code snippet.
EXCEL VBA ON EVENT FULL
Next you can select any event you want from the dropdown on the top right, also pictured below (click on the images to view full size).
![excel vba on event excel vba on event](https://www.homeandlearn.org/images/treeview/treeview_node_click.gif)
Once there, change the dropdown selection at the top that says “(General)” to “Worksheet” as pictured below. The easiest way to create these subscribers is to open your code editor using alt + F11 and selecting the worksheet object you want to edit by double-clicking on it in the project window on the top left. Think of it like a subscription to your favorite sports team’s news site, when something happens you expect the news service you subscribe to will alert you about it. Side note, when you have a piece of code tied to an event, it’s common to say that your subroutine is subscribing to that event. To capture an event when a cell value changes, we actually subscribe to an event called “Worksheet_Change” via a private subroutine. It helps with everything from validating data entry to filtering data sets and even generating reports. I specifically mention this event because it’s the one you’ll likely find the most useful. With that said, let’s dive in! Run Code When Cell Values Change
![excel vba on event excel vba on event](http://www.cpearson.com/images/SubClassEventsWindow.jpg)
![excel vba on event excel vba on event](https://i.pinimg.com/originals/fc/c2/78/fcc2782e9de4d008478f094f4d349893.jpg)
There are more worksheet events that you can use to trigger code than I’ll cover here but I think these are the most important.
EXCEL VBA ON EVENT HOW TO
Therefore, today I want to cover how to run code when cell values change, as well as a handful of other worksheet events that I’ve found useful over the years. That is to say, the user does not need to explicitly trigger your subroutine. Howdee! A common requirement when developing in Excel is the need to have code run while a user interacts with a report.