Wednesday, January 6, 2016

How to Know Right/Left/Middle Click on Command Button in VBA

a. First add a userform to a Workbook (right click VBE(Visual Basic Editor) > Insert > Userform)
b. Add a command button and a label


c. Right Click Command Button, and then click View Code
d. Then choose, Mouseup event


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim str As String


Select Case Button
Case 1
    str = "The left button was pressed."
Case 2
    str = "The right button was pressed."
Case 4
    str = "The middle button was pressed."
Case Else
    str = "The unknown button was pressed."
End Select

Me.Label1 = str

End Sub

The settings for Button are:



Download File

No comments :

Post a Comment