18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Part VI<br />

Programming Excel with VBA<br />

In addition, every event-handler procedure has a predetermined name. You can declare the procedure by<br />

typing it, but a much better approach is to let the VB Editor do it for you. Figure 43.1 shows the code module<br />

for the ThisWorkbook object. To insert a procedure declaration, select Workbook from the objects list<br />

on the left. Then select the event from the procedures list on the right. When you do, you get a procedure<br />

“shell” that contains the procedure declaration line and an End Sub statement.<br />

FIGURE 43.1<br />

The best way to create an event procedure is to let the VBE do it for you.<br />

For example, if you select Workbook from the objects list and Open from the procedures list, the VB Editor<br />

inserts the following (empty) procedure:<br />

Private Sub Workbook_Open()<br />

End Sub<br />

Your event-handler VBA code goes between these two lines.<br />

Some event-handler procedures contain an argument list. For example, you may need to create an eventhandler<br />

procedure to monitor the SheetActivate event for a workbook. If you use the technique<br />

described in the previous section, the VB Editor creates the following procedure:<br />

Private Sub Workbook_SheetActivate(ByVal Sh As Object)<br />

End Sub<br />

This procedure uses one argument (Sh), which represents the activated sheet. In this case, Sh is declared as<br />

an Object data type rather than a Worksheet data type because the activated sheet also can be a chart<br />

sheet.<br />

746

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!