Excel Worksheet Events
On my website there are sample Excel files, many of which use event programming to make things happen. Typing something in a cell, selecting a different cell, activating a worksheet, and many other actions, are Events. Add a bit of code in the background, and things can happen automatically if an event occurs. No need to click a button to run a macro, just change a cell, or refresh a pivot table, and the code for that event will run.
For example, on this worksheet there’s a data validation drop down list in cell B2, where a day of the week can be selected.
When you select a weekday, that changes the worksheet, and could trigger a Worksheet_Change event.
In this example, the Worksheet_Change event code checks the address of the cell that was changed, to see if it was cell B2. If it was, a message is displayed.
View the Worksheet Code
To see the code for a worksheet, right-click on its sheet tab, and click View Code.
The Visual Basic Editor opens, and shows the code for the active sheet.
Multiple Worksheet Events
That code works so well, that you’d like to add another Worksheet_Change event. If you make a change in cell B4, you’d like the date automatically entered in cell B5. Unfortunately, if you add another Worksheet_Change event, you’ll see an error message when you change a cell, because you've used the same procedure name twice on the worksheet.
Combine the Code
Instead of creating separate events with the same name, combine both pieces of code into one. For example, you could use Select Case, and specify what should happen when specific cells are changed.
Some situations will require a more complex solution, and if you experiment a bit, you should be able to include multiple outcomes within a single worksheet event’s code.