Validate a macro execution only if a keyboard key is press

mtheriault2000

Well-known Member
Joined
Oct 23, 2008
Messages
826
Hello

I got a macro that is linked to a button on my sheet. I need a way to prevent false execution. I want to execute the macro only if the "CTRL" key is press at the same time that I click on the button.

I use Excel 2007 and the button are created using Excel < Insert < Illustration < rectangular . The new drawing is then linked to a macro.

Any help appreciated

Martin
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Not sure how to do that, but why not put the following near the start of the macro that is executed from clicking the button:
Rich (BB code):
Dim iCheck as String
iCheck = MsgBox("Do you wish to execute macro " & macroname & "?", vbYesNo)
If iCheck = vbNo Then Exit Sub
Or along similar lines,
Rich (BB code):
Dim iCheck as String
iCheck = MsgBox("Do you wish to execute macro " & macroname & "?", vbYesCancel)
If iCheck = vbCancel Then Exit Sub
 
Upvote 0
Hello & Thanks JackDanIce

The purpose is to create a form that will sent a trading order to an external application. I want a fast and secure way to operate.

That's why, I don't want to use the msgbox validation. Pressing a key while clicking on a button should be fast and safe for my purpose.

Martin
 
Upvote 0
You could remove the command button altogether and run it just on a key combination like:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.OnKey "+^{RIGHT}", "MyMacro"
End Sub

So here ctrl+shift+The Right Arrow will execute the macro named MyMacro
 
Upvote 0
Had a little play and found this.

It seems to work:

Code:
Private Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case 16
            MsgBox "You pressed Shift"
        Case 17
            MsgBox "You pressed Control"
        Case Else
            MsgBox "The KeyCode number of the key you pressed is: " & KeyCode
    End Select
End Sub


Only works once the commandbutton has focus..... meh
 
Last edited:
Upvote 0
Had a little play and found this.

It seems to work:

Code:
Private Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case 16
            MsgBox "You pressed Shift"
        Case 17
            MsgBox "You pressed Control"
        Case Else
            MsgBox "The KeyCode number of the key you pressed is: " & KeyCode
    End Select
End Sub


Only works once the commandbutton has focus..... meh


Thanks Comfy
Sound a good start. I will try to work around that

Martin
 
Upvote 0
Resolve: Validate a macro execution only if a keyboard key is press

Did resolve the problem using GetasynchKeyState function

Code:
Private Declare Function GetAsyncKeyState Lib _
    "user32" (ByVal vKey As Long) As Integer
Public Function ShiftKey(bool)
    If CBool(GetAsyncKeyState(&H10)) Then
        ShiftKey = True
        'MsgBox "Shift key is pressed"
    Else
        ShiftKey = False
    End If
End Function

Sub Close_at_Market()
    If ShiftKey(True) Then
       Sheets("Data_In_Out").Range("o_Order_Type") = "Close at Market"
       Sheets("Data_In_Out").Range("o_New_Value") = "Yes"
       Range("ActionPerform") = "Done"
    Else
       Range("ActionPerform") = "No action Perform"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top