vba Excel 2010 - MouseDown event on Active X Controls Command Button

2.3k views Asked by At

On this page Microsoft explains how to know if a combinaison of Shift, Ctrl and Alt is pressed when MouseDown event occurs:

Private Sub Form_MouseDown(Button As Integer, _ Shift As Integer, X As Single, Y As Single) ShiftTest = Shift And 7 Select Case ShiftTest Case 1 ' or vbShiftMask Print "You pressed the SHIFT key." Case 2 ' or vbCtrlMask Print "You pressed the CTRL key." Case 4 ' or vbAltMask Print "You pressed the ALT key." Case 3 Print "You pressed both SHIFT and CTRL." Case 5 Print "You pressed both SHIFT and ALT." Case 6 Print "You pressed both CTRL and ALT." Case 7 Print "You pressed SHIFT, CTRL, and ALT." End Select End Sub

It works just fine. I have many CommandButtons, would it be possible not to copy paste all these lines and replace them by a function called instead ?

This is the idea but it is not working:

Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) call test(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) End Sub

Private Sub CommandButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) call test(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) End Sub

Same for CommandButton3 and 4...

Private Sub test(Button As Integer, _ Shift As Integer, X As Single, Y As Single) ShiftTest = Shift And 7 Select Case ShiftTest Case 1 ' or vbShiftMask Print "You pressed the SHIFT key." Case 2 ' or vbCtrlMask Print "You pressed the CTRL key." Case 4 ' or vbAltMask Print "You pressed the ALT key." Case 3 Print "You pressed both SHIFT and CTRL." Case 5 Print "You pressed both SHIFT and ALT." Case 6 Print "You pressed both CTRL and ALT." Case 7 Print "You pressed SHIFT, CTRL, and ALT." End Select End Sub

1

There are 1 answers

0
François Bonnefoi On BEST ANSWER
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    test CommandButton1, Shift, X, Y
End Sub

Private Sub CommandButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    test CommandButton2, Shift, X, Y
End Sub