Excel

Chart MouseDown, MouseMove and MouseUp Events

In this tutorial, we’ll discuss MouseDown, MouseMove and MouseUp chart events. These events occur when a mouse button pressed or when the mouse pointer moves over the chart area. The Chart_MouseDown, Chart_MouseMove and Chart_MouseUp procedures must be entered into the code module for the Chart object. Each chart sheet comes with its own built-in module, to open a code module, right-click on the chart sheet tab and select View Code, see Figure:

After you select View Code, you are taken directly into the VBE (Visual Basic Editor) where you can start entering the event procedure code:

Chart_MouseDown

Syntax: Chart_MouseDown(Button, Shift, X, Y)

The Chart_MouseDown event procedure runs when a mouse button is pressed while the pointer is over a chart. This procedure has four arguments:

  1. Button
    Determines which mouse button was pressed:
    • The xlPrimaryButton constant represent the left button (or primary mouse button)
    • The xlSecondaryButton constant represents the right button (or secondary mouse button)
    • You also can use the numeric values to determine which mouse button was press, for example, value 1 for left button, 2 for right button, and 3 for middle button
  2. Shift
    Specifies the state of the Shift, Ctrl, and Alt keys:
    • Value 0 – Not any key selected
    • Value 1Shift was selected
    • Value 2Ctrl was selected
    • Value 4Alt was selected
  3. X
    Specify the x coordinates of the mouse pointer.
  4. Y
    Specify the y coordinates of the mouse pointer.

1. Chart MouseDown Event Example:

Private Sub Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal X As Long, ByVal Y As Long)
 If Button = 1 And Shift = 1 Then
  MsgBox "Primary mouse button down, shift key pressed on x:" & X & " y:" & Y
 End If
End Sub

2. Chart_MouseDown Event Procedure Complete Example:

Private Sub Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal X As Long, ByVal Y As Long)
  Dim sft As String
  Dim btn As String
  Select Case (Shift)
   Case 0
    sft = "none"
   Case 1
    sft = "Shift"
   Case 2
    sft = "Ctrl"
   Case 4
    sft = "Alt"
   Case Else
    sft = Shift & ""
   End Select

  Select Case (Button)
   Case xlPrimaryButton
    btn = "primary"
   Case xlSecondaryButton
    btn = "secondary"
   Case Else
    btn = Button & ""
  End Select

 MsgBox "Mouse " & btn & " button down, " & sft & " key pressed, x:" & X & " y:" & Y

End Sub

Chart_MouseMove

Syntax: Chart_MouseMove(Button, Shift, X , Y)

The Chart_MouseMove event procedure runs when the position of a mouse pointer changes over a chart.

Chart MouseMove Event Example:

Private Sub Chart_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal X As Long, ByVal Y As Long)
 If Button = 1 And Shift = 1 Then
  MsgBox "Primary mouse button MOVE, shift key pressed on x:" & X & " y:" & Y
 End If
End Sub

Chart_MouseUp

Syntax: Chart_MouseUp(Button, Shift, X , Y)

The Chart_MouseUp event procedure runs when a mouse button is released while the pointer is over a chart.

Chart MouseUp Event Example:

Private Sub Chart_MouseUp(ByVal Button As Long, ByVal Shift As Long, ByVal X As Long, ByVal Y As Long)
 If Button = 1 And Shift = 1 Then
  MsgBox "Primary mouse button UP, shift key pressed on x:" & X & " y:" & Y
 End If
End Sub