An action performed by VBA code is called an event. Suppose, some events are executed automatically by Excel that's because of the code written in VBA which is known as Event Handler. The execution of a specified macro can be triggered using an action which is known as an event. For example, if we want to change the color of a specific cell only by clicking on it or if we want to add a text-only by clicking on a button then we can use Events. There are two types of Events one is worksheet events and another one is workbook events which we will discuss in this article.
Worksheet Events
Step 1: Right-click on the sheet tab at the bottom of the Excel screen and select View Code.
Step 2: Select Sheet 1 from the Project-VBA Project tab.
Step 3: Select Worksheet from the drop-down box.
Step 4: Then we can select any events from the right-hand drop-down box.
Now, let's discuss some useful worksheet events.
Activate Events
Syntax: expression.Activate
Expression: Which represents the Worksheet object
The following example is shown to sort the range B1:B8 in descending order when the worksheet is activated.
Deactivate Events
This event takes place when the worksheet is deactivated. The following example is shown to arrange the windows vertically when the worksheet is deactivated.
Change Event
This event is used to change the cell of the worksheet.
Syntax: expression.Change(Target)
Expression: Which represents the Worksheet object
Target: It is the range of the cell
The following example is shown to change the color of the cell to red.
PivotTableUpdate Event
This event is used to update the pivot table in the worksheet.
Syntax: expression.PivotTableUpdate (Target)
Expression: which represents the Worksheet object
Target: Selected Pivot table which we want to update
The following example is shown to update the private table displaying the message of the pivot table has been updated.
Workbook Events
For workbook events we have to follow some following steps:
Step 1: Double-click on ThisWorkbook of Project - VBAProject.
Step 2: Select Workbook from the drop-down box.
Step 3: Then we can select any events from the right-hand drop-down box.
Now, let's discuss some useful workbook events
After Save Event
This event will take place after the workbook is saved.
Syntax: expression.AfterSave(Success)
Success: Returns true if changes have successfully taken otherwise returns false
The following code is to show the message after the workbook is saved.
Sheet Activate Event
This event takes place when the sheet is activated.
Syntax: expression.SheetActivate(sh)
The following code is to show the name of the activated sheet.
Sheet Change Event
This event takes place when the sheet is changed.
Syntax: expression.SheetChange(Sh , Target)
Sh:- The sheet object
Target: Range of the cell to be changed
New Sheet Event
This event takes place when a new sheet is created.
Syntax: expression.SheetActivate(sh)
sh: New sheet as an object
The following code is to move the new sheet to the end of the workbook.
Similar Reads
ActiveX Control in Excel VBA When we are automating an excel sheet with VBA at that time when the user has a requirement for a more flexible design then it's better to use ActiveX Controller. In Excel user has to add the ActiveX Controller manually and ActiveX Controls are used as objects in codes. There are the following types
3 min read
Exhaustive Events Exhaustive Events are a set of events where at least one of the events must occur while performing an experiment. Exhaustive events are a set of events whose union makes up the complete sample space of the experiment. In this article, we will understand the meaning of exhaustive events, its definiti
14 min read
Debugging VBA Code in Excel When we use VBA to write code in excel or when we any programming language to write code we may encounter errors that need to be analyzed, checked, and fixed. To overcome this problem most IDEs and programming languages support debugging. Debugging is the process of executing your programming codes
5 min read
Excel VBA | count() functions Visual Basic for Applications (VBA) is the programming language of Excel and other offices. It is an event-driven programming language from Microsoft. With Excel VBA one can automate many tasks in excel and all other office software. It helps in generating reports, preparing various charts, graphs a
2 min read
VBA Objects in Excel Objects are created from the class. If we take some real-life examples then let's say a Dog is a class where it has its properties and methods like breed, age, and color whereas we can say Dog1 or Dog2 are objects having different breeds, ages, and colors. We can simply say the object is a grouping
4 min read