Excel Tutorials – Worksheet VBA Events
October 29, 2017
Microsoft Excel Worksheets have built-in events that can run visual basic code based on certain action taken by the user within that specific worksheet. These worksheet events allow the users of Microsoft Excel to run code after activating a worksheet or before deactivating a worksheet. These events also allow users the ability to run a code every time a user changes data within a cell or selects a new range of cells. Newer versions of Excel have even created events that allow code to be run when tables and pivot tables are updated or refreshed.
The Worksheet Activate event is a Microsoft Excel event that works on many different versions of Excel. It designed to run a script of code every time the specific worksheet is activated. This event has no required or optional parameters. This event can be used to show a hidden a worksheet upon its activation or it can pop up a login or data form.
The Worksheet Deactivate event is similar to the Worksheet Deactivate event; it also works on many different versions of Excel. This event is designed to run a script of code when a user selects any other worksheet. This event has no required or optional parameters. If the first worksheet is selected and someone selects another worksheet, than the first worksheet will run its Deactivate event. This can be used to hide unused worksheets after they are done be used.
The Worksheet Before Double Click event will run a script of code when a user double clicks on that specific worksheet. This event will work on all versions of Excel. This can be useful if you want to run a macro for a certain cell every time you double click on that cell. You can also use this event to load a macro any time you double click anywhere in the worksheet.
The Worksheet Before Right Click event will run a script of code every time a user right clicks within an Excel Worksheet. This can be useful if you want to create your own context menus for a specific cell or range of cells. This event can also be used to offer multiple menus based whether you hold down the ALT key or the CTRL key.
The Worksheet Calculate event will run every time the Excel has to recalculate that specific worksheet. Basically if you have a formula and one of its variable changes and the solution to the formula changes then this event will run. This can be great if you want to ensure that every time a worksheet data changes are made that you update an external document. You can also set up the external document to update on schedule.
The Worksheet Change event will run every time a cell value or cell format changes within that specific worksheet. Users can use this event to mark any changes in yellow or change a time that marks the most recent update. This event can also be used to allow error checking and ensure that if a variable within a formula changes that the variable is not an incorrect input and that the variable will not give the formula an error.
The Worksheet Follow Hyperlink event will run every time a user selects a hyperlink within the specific workbook to follow. This event can be used on all versions of Excel. This can be useful if you want to see a list of elements of certain web page or if you want to know which pages you have visited through that specific workbook.
The Worksheet Pivot Table Update event will run code every time a pivot table within that specific worksheet is updated or refreshed. This event uses the target parameter to return the updated pivot table as an object. This allows users to edit the pivot table or read data through the use of visual basic. This event can be used to update a cell that shows when the last time the pivot table was updated.
The Worksheet Selection Change event will run code every time a user selects a different cell or range of cells with that specific worksheet. This event uses the target parameter to return the selected range as object. I like to use this event to remove formats of my selection. However this can also be used for many other purposes, such as centering on a selected area or displaying the average of a selection of cells.