Thursday, March 7, 2013

What are Workbook and Worksheet Events in Excel VBA

Excel VBA Events - Workbook and Worksheet Events
An Event is an action initiated either by user action or by other VBA code. An Event Procedure is a Sub procedure that you write, according to the specification of the event, that is called automatically by Excel when an event occurs.
The term 'Excel Events' refers to specific user actions within Excel. 

For example, if the user selects a Worksheet, enters data into a cell, or saves a Workbook, these actions are all Excel events.
Events are linked to Excel Worksheets, Charts, Workbooks, or to the Excel Application itself. Their purpose is to enable the programmer to create vba code to be executed automatically at the time of an event.
--------------------------------------------------------------------------------------------------------
Workbook Events:
The following images shows the various workbook events available.


Examples:
The following is an example for a workbook event 'Workbook_Open' , for this event I I wrote vba code that shows a Welcome message when workbook is open.
Private Sub Workbook_Open()
MsgBox "Hai Welcome To MS-Excel", vbInformation, "Welcome"
End Sub

The following is an example for a workbook event 'Workbook_BeforeClose' , for this event I I wrote vba code that shows a Thank You message before the workbook is close.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
MsgBox "Thank You for Using MS-Excel", vbInformation, "Thanks..See You Again"
End Sub


Note:
In this way you can define other events as you wish.
The scope of  events you  defined for a workbook are limited to that workbook only.
--------------------------------------------------------------------------------------------------------

WorkSheet Events:
The following images shows the various worksheet events available.



Examples:
The following is an example for a work sheet event 'Worksheet_SelectionChange' , for this event I I wrote vba code that shows a Active Cell Address message when when you change a selection in that particular sheet.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

MsgBox "You Have Selected " & ActiveCell.Address
End Sub

Note:
In this way you can define other events as you wish.
The scope of events you  defined for a worksheet are limited to that particular worksheet only.

Thanks, TAMATAM
       


No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog