Excel 2019 Bible

(singke) #1

Part VI: Automating Excel


FIGURE 46.2
Selecting a cell causes the active cell’s row and column to become shaded.

Using the BeforeRightClick event
Normally, when the user right-clicks in a worksheet, a shortcut menu appears. If, for
some reason, you want to prevent the shortcut menu from appearing, you can trap the
RightClick event. The following procedure sets the Cancel argument to True, which
cancels the RightClick event—and, thus, the shortcut menu. Instead, a message box
appears:

Private Sub Worksheet_BeforeRightClick _
(ByVal Target As Range, Cancel As Boolean)
Cancel = True
MsgBox "The shortcut menu is not available."
End Sub

Using Special Application Events
So far, the events discussed in this chapter are associated with an object like a worksheet.
This section discusses two additional events: OnTime and OnKey. These events are not
associated with an object. Instead, you access them by using methods of the Application
object.

Unlike the other events discussed in this chapter, you use a standard VBA module to program the On events in this
section.
Free download pdf