4.4 Events


4.4 Events

VBA is an event-oriented programming language. By this is meant that macros are automatically executed by Excel in reaction to certain events. When you click on a tool, say, or select a menu entry, Excel executes the corresponding procedure on its own. Events are thus triggered by the user via input from the mouse or keyboard. (In traditional programming languages you would be compelled, for example in an infinite loop, to wait continuously for the occurrence of an event, evaluate this event, and in response thereto call the appropriate procedure. This task is taken over by Excel by means of the concept of an event.) The following events can lead to user -defined procedures being called:

  • The selection of a macro via ToolsMacros.

  • The selection of a custom menu item or tool to which a macro is linked (see Chapter 8).

  • Clicking on a control (independent of whether these are located in a form or directly in a worksheet).

  • The recalculation of a worksheet (when user-defined functions are used within it).

  • Opening, saving, and closing a workbook; activating and deactivating a worksheet; exceeding a certain period of time; pressing a key; double clicking on a worksheet, etc.

Pointer  

Clicking on Ctrl+Break usually leads to a program interrupt. This event is not handled in VBA in the framework of event procedures, but rather is treated as an error. Chapter 6 contains a description of how a program interrupt can be responded to.

Automatic Opening of Excel Files

All files located in the directory Xlstart are automatically opened when Excel is launched. In conjunction with an Auto_Open or Workbook_Open event procedure this can be used to carry out various settings when Excel is launched. A particular role within the Xlstart files is played by the "personal" workbook Personal.xls . This file is designed as the location for saving such items as macros, tools, and menu entries that are supposed to be always ready for use. Further information on the precise location of the Xlstart directory as well as the individual configuration of Excel by files stored in that directory can be found in Chapter 5.

Event Procedures

Events in Excel 97/2000/

Starting with Excel 97 explicit procedures have been defined for most objects, which are listed as such in the object browser and are indicated by a yellow lightning bolt. The name of the event procedure is predetermined, being composed of the object name and the event name together, such as Worksheet_Activate . When an analogously named procedure exists, it will be executed automatically by Excel. Therefore, it is no longer necessary to set OnEventXxx properties, as was necessary in Excel 5/7.

This concept can be most easily explained by an example. Open a new workbook, switch into the development environment, and open the module "This Workbook."

To have a procedure executed every time the Excel file is opened, first select in the first listbox the object Workbook and then in the second listbox the event Open . See Figure 4.3. The development environment then automatically generates a code template consisting of Sub and End-Sub instructions. You now need only type in the desired code.

click to expand
Figure 4.3: Selecting the Worksheet event procedure in the code window

The example given here shrinks all other visible Excel windows of other workbooks into icons.

 ' example file VBA-Concepts.xls, Module "This Workbook" Private Sub  Workbook_Open  ()   Dim w As Window   MsgBox "Executing the event procedure Workbook_Open"   For Each w In Application.Windows     If w.Parent.Name <> ActiveWorkbook.Name And w.Visible Then       w.WindowState = xlMinimized     End If   Next End Sub 

The processing of events of worksheets and of the workbook is particularly easy, since in the development environment there is a class module with code templates for the event procedures assigned to each of these objects. Other objects as well can trigger events. Their reception , however, is somewhat more complicated, and this will be discussed further below.

Caution  

In normal modules Application is considered a default object, in that all methods and properties derived from it can be used without Application being explicitly named. In modules associated to Excel objects (such as "This Workbook," worksheets, chart sheets), on the other hand, the object on which the class is based is considered the default object, such as Workbook in the module "This Workbook" or Worksheet( ) in a module for a worksheet. For this reason you must give Application explicitly in class modules if you wish to use these properties or methods.

Events in Excel 5/7

In Excel versions 5 and 7 the event concept was quite different: For an event to be triggered, you first had to link the name of a procedure to OnEventXxx properties or methods of various objects. This procedure was subsequently automatically called when the indicated event occurred. Let us look at an example: The instruction below results in the procedure MacroXy being called whenever the first worksheet is activated.

 Worksheets(1).OnSheetActivate = "MacroXy" 

The automatic invocation of the procedure is halted by linking an empty character string "" to the respective property:

 Worksheets(1).OnSheetActivate = "" 

The actual event procedure can be located in any module:

 Public Sub  MacroXy  ()   MsgBox "Even procedure Excel 5/7" End Sub 
Note  

Beginning with Excel 97 most of the OnEventXxx methods and properties were replaced by new events. However, four keywords have remained unchanged: OnKey , OnRepeat , OnTime , and OnUndo . These four methods are described together with important Excel events in the next section.

Caution  

For reasons of compatibility, all OnEventXxx procedures can still be used. Note, however, that since Excel 97 all OnEventXxx settings are stored in the Excel file and are available when it is opened again. In and of itself this is not a bad idea. The problem is that this behavior is incompatible with Excel 5/7. Many Excel 5/7 programs depend on all the OnEventXxx properties being empty at the time the file is loaded into memory. Yet that is no longer the case.

Auto Procedures in Excel 5/7

Conceptually even older than event procedures are the so-called auto procedures: Whenever a procedure with the name Auto_Open exists in a given module of an Excel file, this procedure will be automatically executed when the file is opened. Analogously, there is an Auto_Close procedure, which is executed when the file is closed (that is, before Excel terminates execution, at the latest).

Note  

Since Excel 97 the auto procedures exist only for reasons of compatibility. The "correct" way of proceeding is instead of Auto_Open or Auto _Close to use the events Open and BeforeClose of the Workbook object (Excel object "This Workbook" in the project explorer).

Note  

Auto procedures are not executed if the opening or closing of an Excel file is carried out by VBA code (that is, by the Workbook methods Open and Close ). For the procedures to be executed anyhow, you must execute the method RunAutoMacros for the affected workbook.

Auto Procedures in Excel-4 Macros

There was even the possibility in the old macro language to set up Auto macros: When a workbook is opened, Excel executes ”even in the current version ”all macros whose name begins with Auto_Open ; for example, Auto_Open, Auto_Open_Test . Analogously, when the file is closed, activated, or deactivated, all macros are executed whose names begin with Auto_Close , Auto_Activate , or Auto_Deactivate .

Deactivating Event Procedures

The one serious disadvantage of the new way that events have been conceived since Excel 97 is that activating and deactivating events using program code is difficult. For example, if you wish temporarily to deactivate the call to a specific event procedure, you have to employ the following code:

 ' in the class module of a worksheet Public activateEvents Private Sub  Worksheet_Activate  ()   If activateEvents <> True Then Exit Sub   MsgBox "Worksheet_Activate " & Me.Name End Sub 

Using the variable activateEvents one can control whether events are processed ( True ) or not ( False ). Note that to change activateEvents you must prefix it with the internal name of the worksheet, for example, Table1.activateEvents=True . (This name must not coincide with the name listed in the sheet tabs. The correct name is the one shown in the project window and that can be changed in the properties window.)

If you wish to deactivate all event procedures temporarily (and not simply one procedure as in the example above), you can set the property EnableEvents for the Application object to False . This can make sense in procedures, for example, whose VBA code instructions would normally trigger events.

Overview of Important Excel Events

About fifty events have been defined in the object model of Excel. Many events are even multiply defined. For example, the Worksheet object has an Activate event, while the Workbook object and the Application object each have a SheetActivate event. This dualism is intentional. For example, if you wish to activate every sheet of a workbook with an event procedure, you do not have to equip each Worksheet object with an Activate procedure. It suffices to supply a single Workbook_SheetActivate procedure, which then is called with each change of sheet. A reference to the corresponding sheet is passed to the procedure as a parameter. If instead of this you set up an event procedure for SheetActivate of the Application object (see the next section), then this procedure will actually be called for every sheet of every workbook.

The following description is limited to the most important events of the objects Workbook and Worksheet , which are particularly simple to execute in the development environment. Furthermore, the four still-supported OnEventXxx events will be treated here in accordance with the old event concept. The syntax summary at the end of this section lists all events for several important Excel objects.

Open and Close File (Events Open/BeforeClose )

After an Excel file has been loaded, the procedure Workbook_Open is executed if it exists. Analogously, when a workbook is closed (that is, when Excel is terminated ), Workbook_Close is executed if this procedure in the module "This Workbook" contains code. These procedures constitute an alternative to the procedures Auto_Open and Auto _Close , which were described above.

Save/Print a File (Events BeforeSave/BeforePrint )

Before a file is saved, the event procedure Workbook_BeforeSave is executed. This makes it possible to update various sorts of information in the workbook immediately before saving, such as to copy the contents of variables into a worksheet. Two parameters are passed to the procedure: SaveAsUI indicates whether the form Save As has been selected. Cancel makes it possible to interrupt the saving process (for example, to give the user the possibility of correcting errors in the workbook that may still exist).

The event procedure Workbook_BeforePrint , which is called whenever part of the workbook is printed, functions similarly. (Surprisingly, there is no separate event for printing a particular worksheet or chart. Also, no information as to which data are to be printed is passed to the BeforePrint procedure.)

Changing Sheets (Events Activate/Deactivate )

A change of sheets occurs when a user activates another sheet within a workbook. Possible reactions in the event procedure can be a validation control for input or an adaptation of the menu or toolbar to the newly displayed sheet.

Activate and Deactivate events can be set for each individual sheet. Further-more, on the workbook level ( Workbook object) the Sheet[De]Activate can be set if you do not wish to write a separate event procedure for each sheet.

If you do write event procedures for individual sheets as well as for the workbook as a whole, the sequence of procedure calls in the case of a change from worksheet 1 to worksheet 2 will look something like this:

EVENT PROCEDURE

OBJECT

Worksheet_Deactivate

worksheet 1

Workbook_SheetDeactivate

workbook, reference to worksheet 1 as parameter

Worksheet_Activate

worksheet 2

Workbook_SheetActivate

workbook 2, reference to worksheet 2 as parameter

You will be informed about the creation of a new workbook, and this will occur via the Workbook_NewSheet event procedure. For some mysterious reason there is no analogous event that is executed before a sheet is deleted.

Changing the Active Workbook (Events Activate/Deactivate )

The events Activate and Deactivate also make an appearance when instead of a sheet within a workbook, a switch is made into the window of another workbook. Otherwise, the functionality is the same as above.

Mouse Events (Events BeforeDoubleClick/BeforeRightClick)

The somewhat peculiar names of the two events of this subsection do not mean that the events clairvoyantly execute themselves as you reach for the mouse but before you have clicked. Even Microsoft has not advanced to this level of user friendliness. The Before refers, rather, to the fact that the event procedure (for sheets or charts ) is executed before Excel reacts to the mouse click.

A Range object, which refers to the cell that has been clicked on, is sent as parameter (as in the case of worksheets). By a change in the Cancel parameter a possible reaction by Excel to a double click can be thwarted. Before a click of the right mouse button is processed, the entries of a pop-up menu can be changed under some circumstances. For the Workbook object the event names are SheetBeforeDoubleClick and SheetBeforeRightClick .

Changing a Cell (Event Change )

The Change event for worksheets and the SheetCalculate event for the workbook enter the picture after a cell is altered or cleared via some input. A Range object of the altered range of cells is passed as parameter. Only the change in the content of the cell, by means the formula bar or directly into the cell, is considered an event, but not a change in formatting settings. If a range of cells is shifted by means of the clipboard, then two Change events occur.

Changing the Selection (Event SelectionChange )

The SelectionChange event occurs when the user changes the set of selected cells. The newly selected region of cells is passed as parameter.

Worksheet Recalculation (Event Calculate )

The Calculate event for worksheets and the SheetCalculate event for the workbook make their appearance after a worksheet is recalculated (even if only a single cell is affected by the recalculation). It can be used, for example, to bring variables that depend on worksheet data up to date.

Window Events (Events WindowActivate, WindowDeactivate, WindowResize)

Surprisingly, the Window object has no associated events. Instead, window events are passed to the associated Workbook object. A change of window or an alteration in a window's size can be detected with the event procedures of these objects.

Mouse Click (Property OnAction )

OnAction was probably the most-used property in Excel 5/7 for linking event procedures to various objects (controls, menu commands, images). Beginning with Excel 97 this property is officially supported only for the Shape object and for various CommandBar objects. For all other objects it has been replaced by " genuine " events. For reasons of compatibility, however, OnAction remains available for these objects as well.

Keyboard Events (Method OnKey )

With the method OnKey a procedure can be defined that will be invoked when a particular key on the keyboard is pressed. Such methods can be established only for the Application object, for which reason a reaction to a key being pressed that depends on which workbook or which sheet is currently active is possible only with a certain amount of effort (namely, by the evaluation of the properties ActiveWorkbook or ActiveSheet ).

OnKey is not a property, but a method. This distinction is necessary because no additional parameters (to the input of the key) can be passed to a property. For this reason the activation of the OnKey event procedure looks a bit different from what occurs with other events:

 Application.OnKey "{F4}", "Macro" '<F4> calls the procedure Macro Application.OnKey "{F4}", ""      'halt automatic calling 

The syntax for specifying keys can be taken from the on-line help for the OnKey method. The result of the example above is that striking the key F4 leads to the procedure Macro being called. Please note that defining a procedure to respond to a particular key combination disables the automatic response of Excel to that combination. Normally, the effect of F4 is to repeat the last command. Now, instead of this, Macro will be called.

The redefining of keyboard input does not hold in all of Excel's aspects. The usual key conventions hold during the input of a formula or in a form. Thus, F4 can continue to be used in the formula bar for switching between absolute and relative addressing.

A further example of OnKey is to be found in the next chapter, where macros are defined for the key combinations Shift+Ctrl+Page Up and Shift+Ctrl+Page Down for jumping to the first, respectively last, sheet of a workbook.

Undo and Repeat (Methods OnUndo, OnRepeat )

With the two methods OnUndo and OnRepeat one can create procedures that Excel carries out when the user executes the command EDITUNDO or EDITREPEAT. These two methods are defined only for the Application object. The use of these methods is appropriate in macros that the user can execute by carrying out various actions.

Two parameters are passed to these methods: The first gives the text shown in the menu and should take the form "Undo: Xxx", respectively "Repeat: Xxx." The second parameter gives the procedure to be called when the menu command is executed:

 Application.OnRepeat "Repeat: analyze data", "MacroXxx" 

The menu texts and procedures provided by these two methods are valid only until the next command is executed (regardless of whether a normal Excel command or a further macro). Thus it is not necessary to clear the entries later by defining an empty string.

The methods must be executed in the last procedure of a chain of procedures. In the course of execution of a Sub procedure the settings of OnUndo , respectively OnRepeat , are cleared upon return to the calling procedure.

If the two methods are not used in a macro, Excel shows in the EDIT menu in the spaces provided for "undo" and "repeat" the entries CAN'T UNDO and REPEAT (MACRO NAME) and then executes the macro once more should the repeat option be chosen .

Time Events (Method OnTime )

The time event, too, is defined only for the Application object. By executing the OnTime method you can start a procedure at a particular time, say at 12 noon or ten minutes from now. At least two parameters are passed to this method: the time at which the procedure should be started and the name of the procedure. The following instruction starts the macro "Macro" thirty seconds after the execution of the instruction.

 Application.OnTime Now + TimeValue("0:00:30"), "Macro" 

With OnTime several procedures can be set for various times. VBA automatically manages the list of procedures to be executed. It is even allowed to set several procedures for the same time.

The execution of the procedures so designated can be postponed if Excel is occupied with other things. For example, a procedure cannot be called while, for example, a form is open, a macro is being executed, Excel is waiting for an MS Query query, or OLE data are being processed in Excel.

The OnTime method recognizes two optional parameters: In the third parameter can be given the latest time at which the procedure will be started. If this moment passes without Excel having had the chance to call the procedure, then the procedure will not be called at all.

By setting the fourth parameter to False a timed procedure can be canceled . For this the planned time as well as the name of the procedure must be given. If VBA does not find the procedure for the given time in the list of such procedures, then an error results.

 Application.OnTime #8:30#, "Macro"            'set Macro for 8:30 Application.OnTime #8:30#, "Macro", , False   'cancel Macro 

If you wish to arrange for the periodic calling of a procedure, you need to give a further call with OnTime within the procedure. Once it has been started, the following procedure changes the text of the status bar and displays there the current time:

 Sub  statusbar_time  ()   Application.OnTime Now + Timevalue("0:00:10"), "statusbar_time"   Application.Statusbar = Now End Sub 

We might mention that it is not particularly easy to stop this procedure once it has been started. Either you can change the name of the procedure (then at the time of the next procedure call, an error will occur with the message that the macro status bar cannot be found), or convert the OnTime instruction via "'" into a comment (then it will end after the next procedure call). If you wish that Excel's normal info text appear in the status bar, you must execute the following instruction in the immediate window:

 Application.Statusbar = False 
Note  

Further information on dealing with the status bar can be found in Chapter 5.

Receiving Events of Arbitrary Objects

For worksheets and workbooks there are code templates for event procedures available. The programming of such event procedures is therefore quite simple and intuitive.

However, there exist countless other objects that can trigger events (even objects from external libraries, which are activated via TOOLSREFERENCES). The Excel objects Application (21 events) and Chart (13) are especially well endowed. In contrast to Visual Basic, where such objects with events can be declared anywhere , in Excel this is possible only in class modules. For this reason the reception of events is, alas, somewhat complicated.

The first step consists in creating a class module (an ordinary module will not suffice) and there defining a public variable of the object class whose events you wish to receive. For this you use the keyword WithEvents . In the listbox of the module window you can thus select all known procedures for this object and add the desired event procedures.

 ' class module "eventclass" Public WithEvents x As objname Private Sub  x_eventname  (parameterlist)   '... the event procedure End Sub 
Pointer  

Actually, class modules anticipate the next section of this chapter, which is concerned with programming new classes.

So that events can actually be received, first an object of the new class and with it an object of the class with the events of interest must be created:

 ' in an arbitrary module Dim obj As New event class 'obj is an object of the "event class" Sub  startevents  ()   Set obj.x = [New] objname    'x is an object of the class "objname" End Sub 

After startevents has been executed, the event procedure in the class module is executed until obj.x or in general obj has been cleared (that is, Set obj = Nothing ).

Example

A concrete example will clarify the situation: The NewWorkbook event of the Excel Application object is to be used to input some text into cell A1 of the first worksheet of every new workbook. (In practice, a similar procedure could be used to carry out various initialization procedures for every new workbook.)

The code consists of two parts : The event procedure is defined in the class module "ClassAppEvents." A reference to the new Workbook object is automatically passed to the procedure NewWorkbook .

 ' file VBA-Concepts.xls, class module "ClassAppEvents" Public WithEvents app As Application Private Sub  app_NewWorkbook  (ByVal wb As Excel.Workbook)   wb.Worksheets(1).[A1] = "This text has been inserted by an " & _                           " event procedure of the Application object " End Sub 

The second part of the test program is located in the module "TestAppEvents." In InitializeApplicationEvents the variable app of the ClassAppEvents object is linked to the Application object. If after the execution of this procedure you add a new workbook (FILENEW), then app_NewWorkbook will be executed for the first time. StopApplicationEvents again ends the automatic changing of new workbooks.

 ' File VBA-Concepts.xls, Module "TestAppEvents" Option Explicit Dim appObject As New ClassAppEvents ' starts the event procedures Sub  InitializeApplicationEvents  ()   Set appObject.app = Application End Sub ' ends the event procedures again Sub  StopApplicationEvents  ()   Set appObject.app = Nothing End Sub 

Generating Event Procedures with Program Code

This example shows how a new worksheet is inserted into a workbook and then a Worksheet_Activate event procedure is generated for this worksheet. The program makes use of the VBE library, with which the VBA development environment can be controlled. The code is designed specifically for advanced programmers and assumes that you have experience in dealing with objects.

Pointer  

Starting with Excel 2000 you can program new classes with their own events (see the next section). The addition of event procedures in Excel files described here has nothing to do with that capability.

The VBE Library

In this book we are not going to give a detailed description of this library. However, to understand this example it is necessary that you at least know the most important objects:

The most interesting methods for this example, CreateEventProc and InsertLine , are associated to the CodeModule . The problem is how to get to these objects: The Name property of an Excel Workbook object is, in fact, not identical to the Name property of the VBComponent object. Further, there are two names, one, which is displayed in Excel in the sheet tab, and a second, which is used in the VBA development environment and can be changed in the properties window. Here the collection of Properties helps further:

 Dim vbc As VBComponent Set vbc = ... Debug.Print vbc.Name                     ' returns the VBA name Debug.Print vbc.Properties("Name").Value ' returns the Excel name 

With the Properties enumeration various object properties can be accessed that come not from the VBE library, but from the underlying object (in this case from a Workbook object of the Excel library).

Note  

Code that uses the VBE library can be executed in Excel 2002 only if the option TOOLSMACROSECURITYTRUSTED SOURCESTRUST ACCESS TO VISUAL BASIC PROJECT permits execution. This new security setting is offered because the VBE library has proved to be of particular interest to virus programmers, and is thus dangerous for the ordinary user.

Example

The program code begins with a test, namely, whether a worksheet with the name "new worksheet" already exists. This test is formulated somewhat unusually: The attempt is simply made to read the Name property of this Workbook object. If no error occurs, then the worksheet in fact exists. (You will learn about On Error in Chapter 6. An alternative way to proceed would be to use a loop to test all existing Workbook objects as to whether their name is identical to the constant newname .)

 ' file VBA-Concepts.xls, Module "TestVBE" Sub  AddWorksheetWithEvents  ()   Const newname$ = "new worksheet"   Dim ws As Worksheet   Dim vbc As VBComponent   Dim wsname$, linenr, dummy   ' test whether a worksheet already exists   On Error Resume Next   dummy = ThisWorkbook.Worksheets(newname).Name   If Err = 0 Then     MsgBox "The sheet " & newname & " already exists. " & _            "Please remove this sheet and " & _            "try again"     Exit Sub   End If   Err = 0   On Error GoTo 0 

If the worksheet does not yet exist, then it will be generated by means of Worksheets.Add . Then the worksheet will be given the name "new worksheet." The following loop runs through all VBComponent objects in the file in order to locate the module whose Properties("Name") property is "new worksheet." The VBA internal name of this object is buffered in the variable wsname . Here ThisWorkbook.VBProject is shorthand for Application.VBE.VBProjects(ThisWorkbook.Name) .

 ' creates a new worksheet Set ws = ThisWorkbook.Worksheets.Add   ws.Name = newname   ' find out the VBE internal name of this sheet   For Each vbc In ThisWorkbook.VBProject.VBComponents     If vbc.Properties("Name").Value = newname Then       wsname = vbc.Name       Exit For     End If   Next 

Now that access to the worksheet's code has been made possible, the insertion of the event procedure proves to be thoroughly unproblematic: First, the procedure CreateEventProc is generated. This method returns the line number of the Sub instruction. One line further down, the instruction MsgBox "event procedure" is added with the method InsertLines .

 ' add event procedure   With ThisWorkbook.VBProject.VBComponents(wsname).CodeModule     linenr = .CreateEventProc("Activate", "Worksheet")     .InsertLines linenr + 1, " MsgBox ""Event procedure"""   End With End Sub 

If you switch into Excel after this macro has been executed and there click on any sheet and then on the new worksheet, the message "Event procedure" will be displayed. It works, by gosh!

Syntax Summary

RECEIVING EVENTS FOR ARBITRARY OBJECTS

Public WithEvents x As objname in the class module "eventclass"

Private Sub x_eventname(param)

    ˜ the event procedure

 

End Sub

 

Dim obj As New eventclass

in an arbitrary module

Sub startevents

 

    Set obj.x = [New] objname

from now on events can be received

End Sub

 

AUTO PROCEDURES

 

Sub Auto_Open() End Sub

launched when the file is opened

Sub Auto_Close() End Sub

launched when the file is closed

Sub Auto_Add() End Sub

launched when the add-in is entered into the list of the add-in manager (Chapter 14)

Sub Auto_Remove() End Sub

launched when the add-in list is removed

APPLICATION EVENTS

 

NewWorkbook

a new workbook is added

SheetActivate

sheet change

SheetBeforeDoubleClick

double click

SheetBeforeRightClick

right mouse button click

SheetCalculate

worksheet was recalculated

SheetChange

cells of the worksheet were changed (content)

SheetDeactivate

sheet change

SheetSelectionChange

selection change

WindowActivate

window change

WindowDeactivate

window change

WindowResize

window resize

WorkbookActivate

change of active workbook

WorkbookAddinInstall

a workbook was installed as an add-in

WorkbookAddinUninstall

a workbook was deinstalled as an add-in

WorkbookBeforeClose

a workbook is about to be closed

WorkbookBeforePrint

a workbook is about to be printed

WorkbookBeforeSave

a workbook is about to be saved

WorkbookDeactivate

change of active workbook

WorkbookNewSheet

new sheet added to the workbook

WorkbookOpen

the workbook was just opened

WORKBOOK EVENTS

 

Activate

the workbook was activated (change of window)

AddinUninstall

the workbook was installed as an add-in

BeforeClose

the workbook is about to be closed

BeforePrint

the workbook is about to be printed

BeforeSave

the workbook is about to be saved

Deactivate

the workbook was activated (change of window)

NewSheet

a new sheet was added

Open

the workbook was just opened

SheetActivate

change of sheet

SheetBeforeDoubleClick

double click in a sheet

SheetBeforeRightClick

right mouse button click in a sheet

SheetCalculate

contents of a sheet were recalculated

SheetChange

input or change to a sheet

SheetDeactivate

sheet change

SheetSelectionChange

change of selection

WindowActivate

change of window

WindowDeactivate

change of window

WindowResize

window resized

WORKSHEET EVENTS

 

Activate

change of sheet

BeforeDoubleClick

double click

BeforeRightClick

click with the right mouse button

Calculate

contents of sheet were recalculated

Change

input or change to a cell

Deactivate

sheet change

SelectionChange

change of selection

CHART EVENTS

 

Activate

change of sheet (or chart)

BeforeDoubleClick

double click

BeforeRightClick

click with right mouse button

Calculate

chart was redrawn based on changed data

Deactivate

change of sheet (or chart)

DragOver

range of cells dragged over chart (but not yet released)

DragPlot

range of cells was released

MouseDown

mouse button was pressed

MouseMove

mouse was moved

MouseUp

mouse button released

Resize

chart size was altered

Select

chart was selected

SeriesChange

change in selected data series

EVENTS IN EXCEL 5/7

 

OnAction "macro"

calls the procedure macro when the object is clicked

OnKey "key", "macro"

after the pressing of a key

OnUndo "menu text", "macro"

after INPUTUNDO

OnRepeat "menu text", "macro"

after INPUTREPEAT

OnTime time, "macro"

at the given time

OnTime time, "macro", end time

as above, but no later than end time

OnTime time, "macro", , False

clear previously mentioned macro

CONVERSION EXCEL 5/7 EXCEL 97, 2000 ETC.

OnAction

Click , Change , etc.

OnCalculate

Calculate, SheetCalculate

OnData

Change, SheetChange

OnDoubleClick

DoubleClick, BeforeDoubleClick, SheetBeforeDoubleClick

OnEntry

Change, SheetChange

OnSheetActivate

Activate, SheetActivate

OnSheetDeactivate

Deactivate, SheetDeactivate

OnWindow

Activate, WindowActivate




Definitive Guide to Excel VBA
Linux Application Development For The Enterprise (Charles River Media Programming)
ISBN: 1584502533
EAN: 2147483647
Year: 2002
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net