Thursday, 7 March 2013

Excel VBA Events - Workbook and Worksheet Events

Excel VBA Macro  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 Reddy
       


No comments:

Post a Comment

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts