To use events with an embed-ded chart, you need to perform the following steps: Create a class module In the VB Editor window, select your project in the Project window and selectInsert
Trang 1Case xlXErrorBars: Id = “XErrorBars”
Case xlYErrorBars: Id = “YErrorBars”
Case xlLegendEntry: Id = “LegendEntry”
Case xlLegendKey: Id = “LegendKey”
Case xlAxis: Id = “Axis”
Case xlMajorGridlines: Id = “MajorGridlines”
Case xlMinorGridlines: Id = “MinorGridlines”
Case xlAxisTitle: Id = “AxisTitle”
Case xlUpBars: Id = “UpBars”
Case xlDownBars: Id = “DownBars”
Case xlSeriesLines: Id = “SeriesLines”
Case xlHiLoLines: Id = “HiLoLines”
Case xlDropLines: Id = “DropLines”
Case xlRadarAxisLabels: Id = “RadarAxisLabels”
Case xlShape: Id = “Shape”
Case xlNothing: Id = “Nothing”
Case Else: Id = “Some unknown thing”
End SelectMsgBox “Selection type:” & IdEnd Sub
This procedure displays a message box that contains a description of the selecteditem When the Selectevent occurs, the ElementIDargument contains an integerthat corresponds to what was selected The Arg1and Arg2arguments provideadditional information about the selected item (see the online help for details) The
Select Casestructure converts the built-in constants to descriptive strings
Enabling events for an embedded chart
As I noted in the previous section, Chartevents are automatically enabled for chartsheets, but not for charts embedded in a worksheet To use events with an embed-ded chart, you need to perform the following steps:
Create a class module
In the VB Editor window, select your project in the Project window and selectInsert ➪ Class Module This will add a new (empty) class module to your project
If you like, you can use the Properties window to give the class module a moredescriptive name
Declare a public Chart object
The next step is to declare a Publicvariable that will be used as the class name
The variable should be of type Chart, and it must be declared in the class moduleusing the WithEventskeyword If you omit the WithEventskeyword, the objectwill not respond to events Following is an example of such a declaration:
Public WithEvents myChartClass As Chart
Trang 2Connect the declared object with your chart
Before your event-handler procedures will run, you must connect the declaredobject in the class module with your embedded chart You do this by declaring anobject of type Class1(or whatever your class module is named) This should be amodule-level object variable, declared in a regular VBA module (not in the classmodule) Here’s an example:
Dim MyChart As New MyChartClass
Then you must write code to actually instantiate the object, such as this instruction:
Set myChart.myChartClass = ActiveSheet.ChartObjects(1).Chart
After the preceding statement is executed, the myChartClassobject in the classmodule points to the first embedded chart on the active sheet Consequently, theevent-handler procedures in the class module will execute when the events occur
Write event-handler procedures for the chart class
In this section, I describe how to write event-handler procedures in the class ule Recall that the class module must contain a declaration such as:
mod-Public WithEvents myChartClass As Chart
After this new object has been declared using the WithEventskeyword, it appears
in the Object drop-down list box in the class module When you select the newobject in the Object box, the valid events for that object are listed in the Proceduredrop-down box on the right (see Figure 18-16)
Figure 18-16: The Procedure list displays valid events for the new Chart object.
Trang 3The following example is a simple event-handler procedure that is executed whenthe embedded chart is activated This procedure simply pops up a message boxthat displays the name of the Chartobject’s parent (which is a ChartObject
object)
Private Sub myChartClass_Activate()MsgBox myChartClass.Parent.Name & “ was activated!”
End SubThe companion CD-ROM contains a workbook that demonstrates the conceptsdescribed in this section
Example: Using Chart events with an embedded chart
The example in this section provides a practical demonstration of the informationpresented in the previous section The example shown in Figure 18-17 consists of anembedded chart that functions as a clickable image map Clicking one of the chartcolumns activates a worksheet that shows detailed data for the region
Figure 18-17: This chart serves as a clickable image map.
The workbook is set up with four worksheets The sheet named Maincontains theembedded chart The other sheets are named North, South, and West Formulas inB1:B4 sum the data in the respective sheets, and this summary data is plotted inthe chart Clicking a column in the chart triggers an event, and the event-handlerprocedure activates the appropriate sheet so the user can view the details for thedesired region
On the CD-ROM
Trang 4The workbook contains a class module named EmbChartClass, and also a normalVBA module named Module1 For demonstration purposes, the Mainworksheetalso contains two buttons: One executes a procedure named EnableChartEvents,the other executes a procedure named DisableChartEvents(both are located in
Module1) In addition, each of the other worksheets contains a button that cutes the ReturntoMainmacro that reactivates the Mainsheet
exe-The complete listing of Module1 follows:
Dim SummaryChart As New EmbChartClassSub EnableChartEvents()
‘ Called by worksheet buttonRange(“A1”).Select
Set SummaryChart.myChartClass = _Worksheets(1).ChartObjects(1).ChartEnd Sub
Sub DisableChartEvents()
‘ Called by worksheet buttonSet SummaryChart.myChartClass = NothingRange(“A1”).Select
End SubSub ReturnToMain()
‘ Called by worksheet buttonSheets(“Main”).ActivateEnd Sub
The first instruction declares a new object variable SummaryChartto be of type
EmbChartClass— which, as you recall, is the name of the class module When theuser clicks the Enable Chart Events button, the embedded chart is assigned to the
SummaryChartobject, which, in effect, enables the events for the chart Listing 18-4shows the class module for EmbChartClass
Clicking the chart generates a MouseDownevent, which executes the
myChartClass_MouseDownprocedure This procedure uses the GetChartElement
method to determine what element of the chart was clicked The GetChartElement
method returns information about the chart element at specified X and Y nates (information that is available via the arguments for the
coordi-myChartClass_MouseDownprocedure
Listing 18-4: Reacting to which column has been clicked on
Public WithEvents myChartClass As ChartPrivate Sub myChartClass_MouseDown(ByVal Button As Long, _ByVal Shift As Long, ByVal X As Long, ByVal Y As Long)
Trang 5Dim IDnum As LongDim a As Long, b As Long
‘ The next statement returns values for
‘ IDNum, a, and bmyChartClass.GetChartElement X, Y, IDnum, a, b
‘ Was a series clicked?
If IDnum = xlSeries ThenSelect Case b
Case 1Sheets(“North”).ActivateCase 2
Sheets(“South”).ActivateCase 3
Sheets(“West”).ActivateEnd Select
End IfRange(“A1”).SelectEnd Sub
This workbook is available on the companion CD-ROM
Charting Tricks
I conclude this chapter by sharing a few charting tricks that I’ve discovered overthe years Some of these techniques may be useful in your applications, and othersare simply for fun At the very least, studying them may give you some new insightsinto the object model for charts
Printing embedded charts on a full page
When an embedded chart is selected, you can print the chart by choosing File ➪Print The embedded chart will be printed on a full page by itself (just as if it were
on a chart sheet), yet it will remain an embedded chart
The following macro prints all embedded charts on the active sheet, and each chart
is printed on a full page:
Sub PrintEmbeddedCharts()For Each chtObj In ActiveSheet.ChartObjectschtObj.Chart.Print
Next chtObjEnd Sub
On the CD-ROM
Trang 6Creating a “dead chart”
Normally, an Excel chart uses data stored in a range Change the data in the range,and the chart is updated automatically In some cases, you may want to “unlink” thechart from its data ranges and produce a “dead chart” (a chart that never changes).For example, if you plot data generated by various what-if scenarios, you may want
to save a chart that represents some baseline so you can compare it with other scenarios
There are two ways to create such a chart:
✦ Paste it as a picture Activate the chart and choose Edit ➪ Copy Then press the
Shift key and select Edit ➪ Paste Picture (the Paste Picture command is able only if you press Shift when you select the Edit menu) The result will be
avail-a picture of the copied chavail-art
✦ Convert the range references to arrays Click on a chart series and then click
the formula bar Press F9 to convert the ranges to an array Repeat this foreach series in the chart
The xl8galry.xls file uses this technique This file is a special workbook used byExcel to store its custom chart formats If you open this workbook, you’ll find 20chart sheets Each chart sheet has “dummy” data, which uses an array rather than
a range as its source
Another way to create a dead chart is to use VBA to assign an array rather than arange to the XValues or Values properties of the Series object
Controlling a data series by hiding data
Figure 18-18 shows a chart that displays daily data for 365 days What if you onlywant to plot, say, the data for February? You could, of course, redefine the chart’sdata range Or, you could take advantage of Excel’s AutoFilter command
By default, a chart does not display data that’s hidden Since Excel’s AutoFilter feature works by hiding rows that don’t meet your criteria, it’s a simple solution.Select Data ➪ Filter ➪ AutoFilter to turn on the AutoFilter mode Each row heading
in the filtered list displays a drop-down arrow Click the arrow and select Customfrom the list Then enter filter criteria that will select the dates that you want toplot The setting shown in Figure 18-19, for example, hides all rows except thosethat have a date in February
Note
Trang 7Figure 18-18: You can use Excel’s AutoFilter feature to plot only a subset of the data.
This workbook is available on the companion CD-ROM
Figure 18-19: Use the Custom AutoFilter
dialog box to filter a list
The resulting chart is shown in Figure 18-20
On the CD-ROM
Trang 8Figure 18-20: Only visible cells are displayed in a chart.
If this technique doesn’t seem to be working, you need to change a setting for thechart Activate the chart, then choose Tools ➪ Options In the Options dialog box,click the Chart tab and place a check mark next to Plot visible cells only Also, toensure that the chart doesn’t disappear when its rows are hidden, set its position-ing to Don’t move or size with cells Use the Format ➪ Selected Chart Area com-mand to change this setting
Storing multiple charts on a chart sheet
Most Excel users who take the time to think about it would agree that a chart sheetholds a single chart Most of the time, that’s a true statement However, it’s cer-tainly possible to store multiple charts on a single chart sheet In fact, Excel letsyou do this directly If you activate an embedded chart and then select Chart ➪Location, Excel displays its Chart Location dialog box If you select the As newsheet option and specify an existing chart sheet as the location, the chart willappear on top of the chart in the chart sheet
Most of the time, you’ll want to add embedded charts to an empty chart sheet To
create an empty chart sheet, select a single blank cell and press F11
One advantage of storing multiple charts on a chart sheet is that you can takeadvantage of the View ➪ Sized with Window command to automatically scale thecharts to the window size and dimensions Figure 18-21 shows an example of achart sheet that contains six embedded charts
Note
Trang 9Figure 18-21: This chart sheet contains six embedded charts.
This workbook is available on the companion CD-ROM
Using linked pictures in a chart
Excel has a feature that lets you display a data table inside of a chart You can selectthis option in Step 3 of the ChartWizard The data table option displays a table thatshows the values used in a chart This is a handy feature, but it’s not very flexible
For example, your formatting options are limited, and you have no control over theposition of the data table (it always appears below the chart)
An alternative to the data table is to use a linked picture of a range Figure 18-22shows an example
On the CD-ROM
Trang 10Figure 18-22: This chart contains a linked picture of a range used
in the chart
To create a linked picture in a chart, first create the chart as you normally would.Then:
1 Select the range that you would like to include in the chart.
2 Select Edit ➪ Copy.
3 Activate the chart.
4 Press Shift, and then select Edit ➪ Paste Picture This pastes an unlinked
pic-ture of the range
5 To create the link, select the picture and then type a reference to the range in
the formula bar The easiest way to do this is to type an equals sign, and thenreselect the range
The picture now contains a live link to the range If you change the values or cellformatting, they will be reflected in the linked picture
This workbook is available on the companion CD-ROM
On the CD-ROM
Trang 11Animated charts
Most people don’t realize it, but Excel is capable of performing simple animations
For example, you can animate shapes and charts Consider the XY chart shown inFigure 18-23
Figure 18-23: A simple VBA procedure will turn this graph into an
interesting animation
The X values (column A) depend on the value in cell A1 The value in each row isthe previous row’s value, plus the value in A1 Column B contains formulas thatcalculate the SIN of the corresponding value in column A The following simple procedure produces an interesting animation It simply changes the value in cell A1,which causes the values in the X and Y ranges to change
Sub AnimateChart()Range(“A1”) = 0For i = 1 To 150Range(“A1”) = Range(“A1”) + 0.035Next i
Range(“A1”) = 0End Sub
The companion CD-ROM contains a workbook that includes this animated chart,plus several other animation examples
On the CD-ROM
Trang 12Creating a hypocycloid chart
Even if you hated your high school trigonometry class, you’ll probably like theexample in this section — which relies heavily on trigonometric functions Theworkbook shown in Figure 18-24 can display an infinite number of dazzling hypocy-cloid curves A hypocycloid curve is the path formed by a point on a circle thatrolls inside of another circle This, as you may recall from your childhood, is thesame technique used in Hasbro’s popular Spirograph toy
Figure 18-24: This workbook generates an infinite number of hypocycloid curves.
This workbook is available on the companion CD-ROM
The chart is an XY chart The X and Y data are generated using formulas stored incolumns A and B The Scrollbar controls at the top let you adjust the three parame-ters that determine the look of the chart These controls are linked to cells B1, B2,and B3 These are controls from the Forms toolbar, and are not ActiveX controls Inaddition, the chart has a Random button that generates random values for thethree parameters
On the CD-ROM
Trang 13The workbook contains only one macro (which follows), which is executed whenthe Random button is clicked This macro simply generates three random numbersbetween 1 and 250 and inserts them into the worksheet.
Sub Random_Click()RandomizeRange(“B1”) = Int(Rnd * 250)Range(“B2”) = Int(Rnd * 250)Range(“B3”) = Int(Rnd * 250)End Sub
Creating a “clock” chart
Figure 18-25 shows an XY chart formatted to look like a clock It not only looks like a
clock, it also functions as a clock I can’t think of a single reason why anyone wouldneed to display a clock like this on a worksheet, but creating the workbook waschallenging and you may find it instructive
Figure 18-25: This clock is fully functional,
and is actually an XY chart in disguise
This workbook is available on the companion CD-ROM
Besides the clock chart, the workbook contains a text box that displays the time as
a normal string, as shown in Figure 18-26 Normally this is hidden, but it can bedisplayed by deselecting the Analog clock check box
On the CD-ROM
Trang 14Figure 18-26: Displaying a digital clock in
a worksheet is much easier, but not as fun
to create
As you explore this workbook from the CD-ROM, here are a few things to keep inmind:
✦ The ChartObjectis named ClockChart, and it covers up a range named
DigitalClock, which is used to display the time digitally
✦ The two buttons on the worksheet are from the Forms toolbar, and each has amacro assigned (StartClockand StopClock)
✦ The check box control (named cbClockType) on the worksheet is from theForms toolbar, not from the Control Toolbox toolbar Clicking the object exe-cutes a procedure named cbClockType_Click, which simply toggles the
Visibleproperty of the ChartObject When it’s invisible, the digital clock isrevealed
✦ The chart is an XY chart with four Seriesobjects These series represent thehour hand, the minute hand, the second hand, and the 12 numbers
✦ The UpdateClockprocedure is executed when the Start Clock button isclicked This procedure determines which clock is visible and performs theappropriate updating
✦ The UpdateClockprocedure uses the OnTimemethod of the Application
object This method lets you execute a procedure at a specific time Beforethe UpdateClockprocedure ends, it sets up a new OnTimeevent that willoccur in one second In other words, the UpdateClockprocedure is calledevery second
✦ The UpdateClockprocedure uses some basic trigonometry to determine theangles at which to display the hands on the clock
✦ Unlike most charts, this one does not use any worksheet ranges for its data.Rather, the values are calculated in VBA and transferred directly to the
Valuesand XValuesproperties of the chart’s Seriesobject
Trang 15Drawing with an XY chart
The final example has absolutely no practical value, but you may find it interesting and maybe even a bit entertaining The worksheet consists of an embedded XY chart,along with a number of controls (these are controls from the Forms toolbar; and arenot ActiveX controls)
This workbook is available on the companion CD-ROM
Clicking one of the arrow buttons draws a line in the chart, the size of which isdetermined by the “step” value, which is set with one of the Spinner controls With
a little practice (and patience), you can create simple sketches Figure 18-27 shows
an example
Figure 18-27: This drawing is actually an embedded XY chart.
Clicking an arrow button executes a macro that adds two values to a range: an Xvalue and a Y value It then redefines two range names (XRangeand YRange) thatare used in the chart’s SERIESformula Particularly handy is the multilevel Undobutton Clicking this button simply erases the last two values in the range, and thenredefines the range names Additional accoutrements include the ability to changethe color of the line, and display “smoothed” lines
On the CD-ROM
Trang 17Understanding Excel’s Events
In several previous chapters in this book, I presented
exam-ples of VBA “event-handler” procedures An event-handlerprocedure is a specially named procedure that is executedwhen a specific event occurs A simple example is the
CommandButton1_Clickprocedure that is executed whenthe user clicks a CommandButton stored on a UserForm or on
✦ A worksheet is activated or deactivated
✦ Data is entered into a cell, or the cell is edited
✦ A workbook is saved
✦ A worksheet is calculated
✦ An object is clicked
✦ The data in a chart is updated
✦ A particular key or key combination is pressed
✦ A cell is double-clicked
✦ A particular time of day occursThis chapter provides comprehensive coverage of the con-cept of Excel events, and I include many examples that youcan adapt to your own needs As you’ll see, understandingevents can give your Excel applications a powerful edge
19C H A P T E R
In This Chapter
An overview of thetypes of events Excelcan monitor
Essential backgroundinformation forworking with eventsExamples of
Workbookevents,
Worksheetevents,
Chartevents, and
UserFormeventsUsing Application
events to monitor allopen workbooksExamples ofprocessing time-based events andkeystroke events
Trang 18Event Types That Excel Can Monitor
Excel is programmed to monitor many different events that occur These eventsmay be classified as the following:
✦ Workbook events Events that occur for a particular workbook Examples of
such events include Open(the workbook is opened or created), BeforeSave
(the workbook is about to be saved), and NewSheet(a new sheet is added)
✦ Worksheet events Events that occur for a particular worksheet Examples
include Change(a cell on the sheet is changed), SelectionChange(the usermoves the cell indicator), and Calculate(the worksheet is recalculated)
✦ Chart events Events that occur for a particular chart These events include
Select(an object in the chart is selected) and SeriesChange(a value of adata point in a series is changed) To monitor events for an embedded chart,you use a class module as demonstrated in Chapter 17
✦ Application events Events that occur for the application (Excel) Examples
include NewWorkbook(a new workbook is created), WorkbookBeforeClose
(any workbook is about to be closed), and SheetChange(a cell in any openworkbook is altered) To monitor Application-level events, you need to use
a class module
✦ UserForm events Events that occur for a particular UserForm or an object
contained on the UserForm For example, a UserForm has an Initialize
event (occurs before the UserForm is displayed), and a CommandButton on aUserForm has a Clickevent (occurs when the button is clicked)
✦ Events not associated with objects The final category consists of two useful
Application-level events that I call “On-” events: OnTimeand OnKey Thesework in a different manner than other events
This chapter is organized according to the preceding list Within each section, I provide examples to demonstrate some of the events
What You Should Know about Events
This section provides some essential information relevant to working with eventsand writing event-handler procedures
Understanding event sequences
As you’ll see, some actions trigger multiple events For example, when you insert anew worksheet into a workbook, this action triggers three Application-levelevents:
Trang 19✦WorkbookNewSheet: Occurs when a new worksheet is added.
✦SheetDeactivateevent: Occurs when the active worksheet is deactivated
✦SheetActivateevent: Occurs when the newly added worksheet is activated
Event sequencing is a bit more complicated than you might think The eventslisted above are Application-level events When adding a new worksheet, addi-tional events occur at the Workbook level and at the Worksheet level
At this point, just keep in mind that events fire in a particular sequence, and ing what the sequence is can be critical when writing event-handler procedures
know-Later in this chapter, I describe how to determine the order of the events that occurfor a particular action (see “Monitoring Application-level events”)
Where to put event-handler procedures
VBA newcomers often wonder why their event-handler procedures aren’t being cuted when the corresponding event occurs The answer is almost always becausethese procedures are located in the wrong place
exe-In the VB Editor window, each project is listed in the Projects window The projectcomponents are arranged in a collapsible list, as shown in Figure 19-1
Figure 19-1: The components for each VBA
Project are listed in the Project window
Each of the following components has its own code module:
✦Sheetobjects (for example, Sheet1, Sheet2, and so on)
✦Chartobjects (that is, chart sheets)
✦ThisWorkbookobject
Note
Trang 20✦ General VBA modules You never put event-handler procedures in a general(that is, nonobject) module.
✦ Class modules
Even though the event-handler procedure must be located in the correct module,the procedure can call other standard procedures stored in other modules Forexample, the following event-handler procedure, located in the module for the
ThisWorkbookobject, calls a procedure named WorkbookSetup,which could bestored in a regular VBA module:
Private Sub Workbook_Open()Call WorkbookSetupEnd Sub
Disabling events does not apply to events triggered by UserForm controls — for
example, the Click event generated by clicking a CommandButton control on aUserForm
Why would you need to disable events? The main reason is to prevent an infiniteloop of cascading events
For example, suppose that cell A1 of your worksheet must always contain a valueless than or equal to 12 You can write some code that is executed whenever data isentered into a cell to validate the cell’s contents In this case, you are monitoringthe Changeevent for a Worksheetusing a procedure named Worksheet_Change.Your procedure checks the user’s entry, and if the entry isn’t less than or equal to
12, it displays a message and then clears that entry The problem is, clearing theentry with your VBA code generates a new Changeevent, so your event-handlerprocedure is executed again This is not what you want to happen, so you need todisable events before you clear the cell, and then enable events again so you canmonitor the user’s next entry
Another way to prevent an infinite loop of cascading events is to declare a StaticBoolean variable at the beginning of your event-handler procedure, such as this:
Static AbortProc As Boolean
Note
Trang 21Whenever the procedure needs to make its own changes, set the AbortProcable to True (otherwise, make sure it is set to False) Insert the following code atthe top of the procedure:
vari-If AbortProc Then AbortProc = FalseExit Sub
Caution
Cross-Reference
Programming Events in Older Versions of Excel
Versions of Excel prior to Office 97 also supported events, but the programming techniquesrequired to take advantage of those were quite different from those described in this chapter
For example, if you have a procedure named Auto_Openstored in a regular VBA module,this procedure will be executed when the workbook is opened Beginning with Excel 97,the Auto_Openprocedure is supplemented by the Workbook_Openevent-handler proce-dure, stored in the code module for the ThisWorkbook object, and executed prior to
Auto_Open.Before Excel 97, it was often necessary to explicitly set up events For example, if youneeded to execute a procedure whenever data was entered into a cell, you would need toexecute a statement such as:
Sheets(“Sheet1”).OnEntry = “ValidateEntry”
This statement instructs Excel to execute the procedure named ValidateEntrywheneverdata is entered into a cell With Excel 97 and later, you simply create a procedure named
Worksheet_Changeand store it in the code module for the Sheet1object
For compatibility reasons, Excel 97 and later versions still support the older event nism (although they no longer are documented in the online help system) If you’re devel-oping applications that will be used only with Excel 97 or later, you’ll definitely want to usethe techniques described in this chapter
Trang 22mecha-Entering event-handler code
Every event-handler procedure has a predetermined name Following are someexamples of event-handler procedure names:
Worksheet_SelectionChangeWorkbook_Open
Chart_ActivateClass_Initialize
You can declare the procedure by typing it manually, but a much better approach is
to let the VB Editor do it for you
Figure 19-2 shows the code module for the ThisWorkbookobject To insert a dure declaration, select Workbookfrom the objects list on the left Then select theevent from the procedures list on the right When you do so, you’ll get a procedure
proce-“shell” that contains the procedure declaration line and an End Substatement
Figure 19-2: The best way to create an event procedure is
to let the VB Editor do it for you
For example, if you select Workbookfrom the objects list and Openfrom the dures list, the VB Editor will insert the following (empty) procedure:
proce-Private Sub Workbook_Open()End Sub
Your code, of course, goes between these two statements
Trang 23Event-handler procedures that use arguments
Some event-handler procedures use an argument list For example, you may need tocreate an event-handler procedure to monitor the SheetActivateevent for a work-book If you use the technique described in the previous section, the VB Editor willcreate the following procedure:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)End Sub
This procedure uses one argument (Sh), which represents the sheet that was vated In this case, Shis declared as an Objectdatatype rather than a Worksheet
acti-datatype That’s because the activated sheet can also be a chart sheet
Your code can, of course, make use of the data passed as an argument The ing procedure is executed whenever a sheet is activated It displays the type andname of the activated sheet by using VBA’s TypeNamefunction and accessing the
follow-Nameproperty of the object passed in the argument:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)MsgBox TypeName(Sh) & vbCrLf & Sh.Name
End Sub
Several event-handler procedures use a Booleanargument named Cancel Forexample, the declaration for a workbook’s BeforePrintevent is as follows:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
The value of Cancelpassed to the procedure is False However, your code cansetCancelto True, which will cancel the printing The following exampledemonstrates:
Private Sub Workbook_BeforePrint(Cancel As Boolean)Msg = “Have you loaded the 5164 label stock?”
Ans = MsgBox(Msg, vbYesNo, “About to print ”)
If Ans = vbNo Then Cancel = TrueEnd Sub
The Workbook_BeforePrintprocedure is executed before the workbook isprinted This routine displays the message box shown in Figure 19-3 If the userclicks the No button, Cancelis set to True and nothing is printed
The BeforePrint event also occurs when the user previews a worksheet
Note
Trang 24Figure 19-3: You can cancel the print operation by changing
the Cancelargument
Unfortunately, Excel does not provide a sheet-level BeforePrintevent Therefore,your code cannot determine what is about to be printed
Event Action that triggers the event
Activate A workbook is activated.
AddinInstall A workbook is installed as an add-in.
AddinUninstall A workbook is uninstalled as an add-in.
BeforeClose A workbook is about to be closed.
BeforePrint A workbook (or anything in it) is about to be printed
or previewed.
BeforeSave A workbook is about to be saved.
Deactivate A workbook is deactivated.
NewSheet A new sheet is created in a workbook.
Open A workbook is opened.
PivotTableCloseConnection* An external data source connection for a pivot table
is closed.
PivotTableOpenConnection* An external data source connection for a pivot table
is opened.
SheetActivate Any sheet is activated.
SheetBeforeDoubleClick Any worksheet is double-clicked This event occurs
before the default double-click action.
Trang 25Event Action that triggers the event
SheetBeforeRightClick Any worksheet is right-clicked This event occurs
before the default right-click action.
SheetCalculate Any worksheet is calculated (or recalculated).
SheetChange Any worksheet is changed by the user or by an
external link.
SheetDeactivate Any sheet is deactivated.
SheetFollowHyperlink A hyperlink on a sheet is clicked.
SheetPivotTableUpdate* A pivot table is updated with new data.
SheetSelectionChange The selection on any worksheet is changed.
WindowActivate Any workbook window is activated.
WindowDeactivate Any workbook window is deactivated.
WindowResize Any workbook window is resized.
* These events occur only in Excel 2002, and are not supported in previous versions.
If you need to monitor events for any workbook, you need to work with
Application-level events (see “Application Events,” later in this chapter)
The remainder of this section presents examples of using Workbook-levelevents All of the example procedures that follow must be located in thecode module for the ThisWorkbookobject If you put them into any othertype of code module, they won’t work
The Open event
One of the most common events that is monitored is the Openevent for a book This event is triggered when the workbook (or add-in) is opened, and exe-cutes the Workbook_Openprocedure A Workbook_Openprocedure can do almostanything, and often is used for tasks such as:
work-✦ Displaying welcome messages
✦ Opening other workbooks
✦ Setting up custom menus or toolbars
✦ Activating a particular sheet or cell
✦ Ensuring that certain conditions are met For example, a workbook mayrequire that a particular add-in is installed
✦ Setting up certain automatic features For example, you can define key nations (see “The OnKeyevent” later in this chapter)
combi-
Cross-Reference
Trang 26✦ Setting a worksheet’s ScrollAreaproperty (which isn’t stored with theworkbook).
✦ Setting UserInterfaceOnlyprotection for worksheets, so your code canoperate on protected sheets This setting is an argument for the Protect
method and is not stored with the workbook
If the user holds down the Shift key when opening a workbook, the workbook’sWorkbook_Openprocedure will not execute
Following is a simple example of a Workbook_Openprocedure It uses VBA’s
Weekdayfunction to determine the day of the week If it’s Friday, a message boxappears, reminding the user to perform a weekly file backup If it’s not Friday, nothing happens
Private Sub Workbook_Open()
If Weekday(Now) = vbFriday ThenMsg = “Today is Friday Make sure that you “Msg = Msg & “do your weekly backup!”
MsgBox Msg, vbInformationEnd If
End Sub
The Activate event
The following procedure is executed whenever the workbook is activated This procedure simply maximizes the active window
Private Sub Workbook_Activate()ActiveWindow.WindowState = xlMaximizedEnd Sub
The SheetActivate event
The following procedure is executed whenever the user activates any sheet in theworkbook If the sheet is a worksheet, the code simply selects cell A1 If the sheet isnot a worksheet, nothing happens This procedure uses VBA’s TypeNamefunction toensure that the activated sheet is a worksheet (as opposed to a chart sheet)
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If TypeName(Sh) = “Worksheet” Then _Range(“A1”).Select
End Sub
An alternative method to avoid the error that occurs when you try to select a cell
on a chart sheet is to simply ignore the error
Caution
Trang 27Private Sub Workbook_SheetActivate(ByVal Sh As Object)
On Error Resume NextRange(“A1”).SelectEnd Sub
The NewSheet event
The following procedure is executed whenever a new sheet is added to the book The sheet is passed to the procedure as an argument Since a new sheet can
work-be either a worksheet or a chart sheet, this procedure determines the sheet type Ifit’s a worksheet, the code inserts a date and time stamp in cell A1
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If TypeName(Sh) = “Worksheet” Then _Range(“A1”) = “Sheet added “ & Now()End Sub
The BeforeSave event
The BeforeSaveevent occurs before the workbook is actually saved As you know,using the File ➪ Save command sometimes brings up the Save As dialog box Thishappens if the workbook has never been saved, or if it was opened in read-onlymode
When the Workbook_BeforeSaveprocedure is executed, it receives an argument(SaveAsUI) that lets you identify if the Save As dialog box will be displayed Thefollowing example demonstrates:
Private Sub Workbook_BeforeSave _(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI ThenMsgBox “Click OK to display the Save As dialog box.”
End IfEnd Sub
When the user attempts to save the workbook, the Workbook_BeforeSavedure is executed If the save operation will bring up Excel’s Save As dialog box, the
proce-SaveAsUIvariable is True The procedure above checks this variable and displays
a message only if the Save As dialog box will be displayed If the procedure sets the
Cancelargument to True, the file will not be saved
The Deactivate event
The following example demonstrates the Deactivateevent This procedure is cuted whenever the workbook is deactivated, and essentially never lets the userdeactivate the workbook When the Deactivateevent occurs, the code re-activatesthe workbook and displays a message
Trang 28exe-Private Sub Workbook_Deactivate()Me.Windows(1).Activate
MsgBox “Sorry, you may not leave this workbook”
End Sub
I do not recommend using procedures — such as this one — that attempt to “takeover” Excel It can be very frustrating and confusing for the user Rather, I wouldrecommend training the user how to use your application correctly
This simple example illustrates the importance of understanding event sequences
If you try out this procedure, you’ll see that it works well if the user attempts toactivate another workbook However, it’s important to understand that the work-book Deactivateevent is also triggered by the following actions:
✦ Closing the workbook
✦ Opening a new workbook
✦ Minimizing the workbook
In other words, this procedure may not perform as it was originally intended Itdoes prevent the user from activating a different workbook directly, but he or shecan close the workbook, open a new one, or minimize the workbook The messagebox will still appear, but the actions will occur anyway
The BeforePrint event
The BeforePrintevent occurs when the user requests a print or a print preview,but before the printing or previewing actually occurs The event uses a Cancel
argument, so your code can cancel the printing or previewing by setting the Cancel
variable to True Unfortunately, there is no way to determine if the BeforePrint
event was triggered by a print request or a preview request
Before Excel 2002, users were often dismayed to discover that it was not possible
to print a workbook’s full path in the page header or footer Excel 2002 solves thisproblem by adding a new option to the Header and Footer dialog boxes (accessedfrom the Page Setup dialog box)
If you’re still using an older version of Excel, the only solution is to write code thatinserts the workbook’s path into the header or footer The Workbook_BeforePrint
event is perfect for this The following code demonstrates:
Private Sub Workbook_BeforePrint(Cancel As Boolean)For Each sht In ThisWorkbook.Sheets
Trang 29This procedure loops through each sheet in the workbook and sets the LeftFooter
property of the PageSetupobject to the FullNameproperty of the workbook(which is the filename and path) It also sets the font size to 8 points
This example exposes an inconsistency in Excel’s object model To change the fontsize of header or footer text, you must use a string that contains a special format-ting code In the example above, “&8” is the code for 8-point font Ideally, thereshould be a Font object available for page headers and footers To find out theother formatting codes available, consult the online help (or record a macro whileyou access the Page Setup dialog box)
When testing BeforePrint event-handlers, you can save time (and paper) bypreviewing rather than actually printing
The BeforeClose event
The BeforeCloseevent occurs before a workbook is closed This event is oftenused in conjunction with a Workbook_Openevent-handler For example, you mightuse the Workbook_Openprocedure to create a custom menu for your workbook,and then use the Workbook_BeforeCloseprocedure to delete the custom menuwhen the workbook is closed That way, the custom menu is available only whenthe workbook is open
As you know, if you attempt to close a workbook that hasn’t been saved, Excel plays a prompt asking if you want to save the workbook before closing, as shown inFigure 19-4
dis-Figure 19-4: When this message appears,
Workbook_BeforeClosehas already done its thing
A potential problem can arise because by the time the user sees this message, theBeforeClose event has already occurred — which means that yourWorkbook_BeforeCloseprocedure has already executed
Consider this scenario: You need to display a custom menu when a particular book is open Therefore, your workbook uses a Workbook_Openprocedure to cre-ate the menu when the workbook is opened, and it uses a Workbook_BeforeClose
work-procedure to remove the menu when the workbook is closed These two handler procedures follow Both of these call other procedures, which are notshown here
event-Caution Tip Note
Trang 30Private Sub Workbook_Open()Call CreateMenu
End SubPrivate Sub Workbook_BeforeClose(Cancel As Boolean)Call DeleteMenu
End Sub
As I noted above, Excel’s “Do you want to save ” prompt occurs after the
Workbook_BeforeCloseevent-handler runs So if the user clicks Cancel, the book remains open, but the custom menu item has already been deleted!
work-One solution to this problem is to bypass Excel’s prompt and write your own code
in the Workbook_BeforeCloseprocedure to ask the user to save the workbook.The following code demonstrates:
Private Sub Workbook_BeforeClose(Cancel As Boolean)Dim Msg As String
If Me.Saved ThenCall DeleteMenuExit Sub
ElseMsg = “Do you want to save the changes you made to “Msg = Msg & Me.Name & “?”
Ans = MsgBox(Msg, vbQuestion + vbYesNoCancel)Select Case Ans
Case vbYesMe.SaveCall DeleteMenuCase vbNo
Me.Saved = TrueCall DeleteMenuCase vbCancelCancel = TrueEnd Select
End IfEnd Sub
This procedure checks the Savedproperty of the Workbookobject to determine ifthe workbook has been saved If so, no problem — the DeleteMenuprocedure isexecuted and the workbook is closed But if the workbook has not been saved, theprocedure displays a message box that duplicates the one that Excel would nor-mally show If the user clicks Yes, the workbook is saved, the menu is deleted, andthe workbook is closed If the user clicks No, the code sets the Savedproperty ofthe Workbookobject to True (but doesn’t actually save the file), and deletes themenu If the user clicks Cancel, the BeforeCloseevent is canceled and the proce-dure ends without deleting the menu
Trang 31Event Action that triggers the event
Activate The worksheet is activated.
BeforeDoubleClick The worksheet is double-clicked.
BeforeRightClick The worksheet is right-clicked.
Calculate The worksheet is calculated (or recalculated).
Change Cells on the worksheet are changed by the user or by an
external link.
Deactivate The worksheet is deactivated.
FollowHyperlink A hyperlink on the sheet is clicked.
PivotTableUpdate* A pivot table on the sheet is updated.
SelectionChange The selection on the worksheet is changed.
* This event occurs only in Excel 2002, and is not supported in previous versions.
Remember that the code for a worksheet event must be stored in the code modulefor the specific worksheet
To quickly activate the code module for a worksheet, right-click the sheet tab, andchoose View Code
The Change event
The Changeevent is triggered when any cell in a worksheet is changed by the user
or by an external link The Changeevent is not triggered when a calculation ates a different value for a formula, or when an object is added to the sheet
gener-Tip Note
Trang 32When the Worksheet_Changeprocedure is executed, it receives a Rangeobject asits Targetargument This Rangeobject represents the changed cell or range thattriggered the event The following example displays a message box that shows theaddress of the Targetrange:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)MsgBox “Range “ & Target.Address & “ was changed.”
End Sub
To get a better feel for the types of actions that generate a Changeevent for a sheet, enter the preceding procedure in the code module for a Worksheetobject.After entering this procedure, activate Excel and make some changes to the work-sheet using various techniques Every time the Changeevent occurs, you’ll see amessage box that displays the address of the range that was changed
work-When I ran this procedure, I discovered some interesting quirks Some actions thatshould trigger the event don’t, and other actions that should not trigger the event do!
✦ Changing the formatting of a cell does not trigger the Changeevent (as
expected), but using the Edit ➪ Clear Formats command does trigger the
event
✦ Adding, editing, or deleting a cell comment does not trigger the Changeevent
✦ Pressing Del generates an event even if the cell is empty to start with
✦ Cells that are changed by using Excel commands may or may not trigger the
Changeevent For example, the Data ➪ Form command and the Data ➪ Sortcommand do not trigger the event But the Tools ➪ Spelling command and theEdit ➪ Replace command do trigger the Changeevent
✦ If your VBA procedure changes a cell, it does trigger the Changeevent
As you can see from the preceding list, it’s not a good idea to rely on the Change
event to detect cell changes for critical applications
To add to the confusion, triggers for the Change event vary, depending on the sion of Excel For versions earlier than Excel 2002, filling a range using the Edit ➪Fill command does not generate a Change event Nor does using the Edit ➪Delete command to delete cells
ver-Monitoring a specific range for changes
The Changeevent occurs when any cell on the worksheet is changed But in mostcases all you care about are changes made to a specific cell or range When the
Worksheet_Changeevent-handler procedure is called, it receives a Rangeobject
as its argument This Rangeobject represents the cell or cells that were changed
Note
Trang 33Assume that your worksheet has a range named InputRange, and you would like tomonitor changes made only within this range There is no Changeevent for a Range
object, but you can perform a quick check within the Worksheet_Changedure The following procedure demonstrates:
proce-Private Sub Worksheet_Change(ByVal Target As Excel.Range)Dim VRange As Range
Set VRange = Range(“InputRange”)
If Not Intersect(Target, VRange) Is Nothing Then _MsgBox “A changed cell is in the input range.”
End Sub
This example uses a Rangeobject variable named VRange, which represents theworksheet range that you are interested in monitoring for changes The procedureuses VBA’s Intersectfunction to determine if the Targetrange (passed to theprocedure in its argument) is contained in VRange The Intersectfunction returns
an object that consists of all the cells that are contained in both of its arguments Ifthe Intersectfunction returns Nothing, then the ranges have no cells in common
The Notoperator is used so the expression returns Trueif the ranges do have at
least one cell in common Therefore, if the changed range has any cells in commonwith the range named InputRange, a message box is displayed Otherwise, the procedure ends and nothing happens
Tracking cell changes in a comment
The following example adds a notation to the cell’s comment each time the cell ischanged (as determined by the Changeevent) The state of a CheckBox, embedded
on the worksheet, determines if the change is added to the comment Figure 19-5shows an example of a comment for a cell that has been changed several times
Figure 19-5: The Worksheet_Changeprocedure appends the comment with each cell change
Trang 34This example is available on the companion CD-ROM.
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If CheckBox1 ThenFor Each cell In TargetWith cell
On Error Resume NextOldText = Comment.Text
If Err <> 0 Then AddCommentNewText = OldText & “Changed to “ & cell.Text & _
“ by “ & Application.UserName & “ at “ & Now &vbLf
.Comment.Text NewText.Comment.Visible = True.Comment.Shape.SelectSelection.AutoSize = True.Comment.Visible = FalseEnd With
Next cellEnd IfEnd Sub
Because the object passed to the Worksheet_Changeprocedure can consist of amulticell range, the procedure loops through each cell in the Targetrange If thecell doesn’t already contain a comment, one is added Then new text is appended tothe existing comment text (if any)
This example is primarily for instructional purposes If you really need to trackchanges in a worksheet, Excel’s Tools ➪ Track Changes feature does a much better job
Validating data entry
Excel’s Data Validation feature is a useful tool, but it suffers from a potentially ous problem When you paste data to a cell that uses data validation, the pastedvalue not only fails to get validated, it also deletes the validation rules associatedwith the cell! This fact makes the Data Validation feature practically worthless forcritical applications In this section, I demonstrate how you can make use of the
seri-Changeevent for a worksheet to create your own data validation procedure
The companion CD-ROM contains two versions of this example One uses theEnableEventsproperty to prevent cascading Change events, the other uses aStatic variable (see “Disabling events,” earlier in this chapter)
Listing 19-1 presents a procedure that is executed when a cell is changed by theuser The validation is restricted to the range named InputRange Values enteredinto this range must be integers between 1 and 12
On the CD-ROM Note
On the CD-ROM
Trang 35Listing 19-1: Determining whether a cell entry
is to be validated
Private Sub Worksheet_Change(ByVal Target As Excel.Range)Dim VRange As Range, cell As Range
Dim Msg As StringDim ValidateCode As VariantSet VRange = Range(“InputRange”)For Each cell In Target
If Union(cell, VRange).Address = VRange.Address ThenValidateCode = EntryIsValid(cell)
If ValidateCode = True ThenExit Sub
ElseMsg = “Cell “ & cell.Address(False, False) _
& “:”
Msg = Msg & vbCrLf & vbCrLf & ValidateCodeMsgBox Msg, vbCritical, “Invalid Entry”
Application.EnableEvents = Falsecell.ClearContents
cell.ActivateApplication.EnableEvents = TrueEnd If
End IfNext cellEnd Sub
The Worksheet_Changeprocedure creates a Range object (named VRange) thatrepresents the worksheet range that is validated Then it loops through each cell
in the Targetargument, which represents the cell or cells that were changed Thecode determines if each cell is contained in the range to be validated If so, it passesthe cell as an argument to a custom function (EntryIsValid), which returns True ifthe cell is a valid entry
If the entry is not valid, the EntryIsValidfunction returns a string that describesthe problem, and the user is informed via a message box (see Figure 19-6) Whenthe message box is dismissed, the invalid entry is cleared from the cell and the cell
is activated Notice that events are disabled before the cell is cleared If events werenot disabled, clearing the cell would produce a Changeevent that causes an end-less loop
Figure 19-6: This message box describes the problem
when the user makes an invalid entry
Trang 36The EntryIsValidfunction procedure is presented in Listing 19-2.
Listing 19-2: Validating an entry just made
into a restricted range
Private Function EntryIsValid(cell) As Variant
‘ Returns True if cell is an integer between 1 and 12
‘ Otherwise it returns a string that describes the problem
‘ Numeric?
If Not WorksheetFunction.IsNumber (cell) ThenEntryIsValid = “Non-numeric entry.”
Exit FunctionEnd If
‘ Integer?
If CInt(cell) <> cell ThenEntryIsValid = “Integer required.”
Exit FunctionEnd If
‘ Between 1 and 12?
If cell < 1 Or cell > 12 ThenEntryIsValid = “Valid values are between 1 and 12.”Exit Function
End If
‘ It passed all the testsEntryIsValid = TrueEnd Function
The SelectionChange event
The following procedure demonstrates the SelectionChangeevent It’s executedwhenever the user makes a new selection on the worksheet
Private Sub Worksheet_SelectionChange(ByVal Target _
As Excel.Range)Cells.Interior.ColorIndex = xlNoneWith ActiveCell
.EntireRow.Interior.ColorIndex = 36.EntireColumn.Interior.ColorIndex = 36End With
End Sub
This procedure shades the row and column of the active cell, which makes it veryeasy to identify the active cell The first statement removes the background colorfor all cells in the worksheet Next, the entire row and column of the active cell isshaded light yellow Figure 19-7 shows the shading in effect; trust me, it’s yellow
Trang 37Figure 19-7: Moving the cell cursor causes the active cell’s row and column
to be shaded
This example is available on the companion CD-ROM
You won’t want to use the procedure if your worksheet contains backgroundshading, because it will be wiped out
The BeforeRightClick event
When the user right-clicks in a worksheet, Excel displays a shortcut menu If, forsome reason, you’d like to prevent the shortcut menu from appearing in a particu-lar sheet, you can trap the RightClickevent The following procedure sets the
Cancelargument to True, which cancels the RightClickevent and thereby cancels the shortcut menu Instead, a message box is displayed
Private Sub Worksheet_BeforeRightClick _(ByVal Target As Excel.Range, Cancel As Boolean)Cancel = True
MsgBox “The shortcut menu is not available.”
End SubChapter 24 describes other methods to disable shortcut menus
Cross-Reference
Caution
On the CD-ROM
Trang 38Events Recognized by a Chart Sheet
Event Action that triggers the event
Activate The chart sheet or embedded chart is activated.
BeforeDoubleClick The chart sheet or an embedded chart is double-clicked This
event occurs before the default double-click action.
BeforeRightClick The chart sheet or an embedded chart is right-clicked The event
occurs before the default right-click action.
Calculate New or changed data is plotted on a chart.
Deactivate The chart is deactivated.
DragOver A range of cells is dragged over a chart.
DragPlot A range of cells is dragged and dropped onto a chart.
MouseDown A mouse button is pressed while the pointer is over a chart MouseMove The position of the mouse pointer changes over a chart.
MouseUp A mouse button is released while the pointer is over a chart Resize The chart is resized.
Select A chart element is selected.
SeriesChange The value of a chart data point is changed.
Application Events
In previous sections, I discussed Workbookevents and Worksheetevents Thoseevents are monitored for a particular workbook If you would like to monitor eventsfor all open workbooks or all worksheets, you use Applicationlevel events
Cross-Reference
Trang 39Using the Object Browser to Locate Events
The Object Browser is a useful tool that can help you learn about objects and their ties and methods It can also help you find out which objects support a particular event Forexample, say you’d like to find out which objects support the MouseMoveevent Activate the
proper-VB Editor and press F2 to display the Object Browser window Make sure <All Libraries> is
selected and then type MouseMove and click the binoculars icon (see the accompanying
as that for another belonging to a different library or class — although they probably do notshare the same functionality So be sure to click each item in the Object Browser list andcheck the status bar at the bottom of the list for the syntax You might find, for instance, thatone class or library treats an event differently
Trang 40Creating event-handler procedures to handle Application events alwaysrequires a class module and some set-up work.
Table 19-4 lists the Applicationevents, with a brief description of each
Table 19-4
Events Recognized by the Application Object
Event Action that triggers the event
NewWorkbook A new workbook is created.
SheetActivate Any sheet is activated.
SheetBeforeDoubleClick Any worksheet is double-clicked This event occurs
before the default double-click action.
SheetBeforeRightClick Any worksheet is right-clicked This event occurs before
the default right-click action.
SheetCalculate Any worksheet is calculated (or recalculated).
SheetChange Cells in any worksheet are changed by the user or by an
external link.
SheetDeactivate Any sheet is deactivated.
SheetFollowHyperlink A hyperlink is clicked.
SheetPivotTableUpdate* Any pivot table is updated.
SheetSelectionChange The selection changes on any worksheet except a chart
sheet.
WindowActivate Any workbook window is activated.
WindowDeactivate Any workbook window is deactivated.
WindowResize Any workbook window is resized.
WorkbookActivate Any workbook is activated.
WorkbookAddinInstall A workbook is installed as an add-in.
WorkbookAddinUninstall Any add-in workbook is uninstalled.
WorkbookBeforeClose Any open workbook is closed.
WorkbookBeforePrint Any open workbook is printed.
WorkbookBeforeSave Any open workbook is saved.
WorkbookDeactivate Any open workbook is deactivated.
WorkbookNewSheet A new sheet is created in any open workbook.
Note