Microsoft Office Tutorials and References
In Depth Information
Chapter 19: Understanding Excel’s Events
This procedure uses one argument ( Sh ), which represents the sheet that was activated. In this
case, Sh is declared as an Object data type rather than a Worksheet data type because the
activated sheet can also be a chart sheet.
Your code can use the data passed as an argument. The following procedure is executed
whenever a sheet is activated. It displays the type and name of the activated sheet by using VBA’s
TypeName function and accessing the Name property of the object passed in the argument:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
MsgBox TypeName(Sh) & vbCrLf & Sh.Name
End Sub
Figure 19-3 shows the message that appears when Sheet1 is activated.
Figure 19-3: This message box was triggered by a SheetActivate event.
Several event-handler procedures use a Boolean argument named Cancel . For example, the
declaration for a workbook’s BeforePrint event is as follows:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
The value of Cancel passed to the procedure is False . However, your code can set Cancel to
True , which will cancel the printing. The following example demonstrates this:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim Msg As String
Dim Ans As Integer
Msg = “Have you loaded the 5164 label stock?”
 
Search JabSto ::




Custom Search