Executing a macro whith click over a button and ctrl key

mtheriault2000

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

I got a conception problem with a verification procedure for calling a macro.

Situation:
I'm building an Excel sheet form to send trade orders. As I'm dealing with real money, I want to add some security to prevent false triggers of an order.

I want to execute a macro when I click over a button and press at the same time the left ctrl key. To do so, I use this function to verify the state of the key.
Code:
Public Function CTRLKey(bool)
    If CBool(GetAsyncKeyState(&HA2)) Then
        CTRLKey = True
     Else
        CTRLKey = False
    End If
End Function
2 problems:
1- When i use the combinaison of a click over a button and the CTRL left key, Excel entered in Edit mode for the button. Is there a way to prevent that?

2- The GetAsyncKeyState will be triggered if the keyswitch as been press since the last verification. Meaning: If I press the key, then release it, then Click on my button, the GetAsyncKeyState will give true because the ctrl key as been hit.
Question: How could i get a true at the moment key states?


Martin
Hope I'm clear enough :rolleyes:
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
To overcome your first problem use a CommandButton from the Control Toolbox rather than one from the Forms Toolbar.
 
Upvote 0
You can also easily trap the Key code with a Toolbox toolbar command button.

Code:
Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyControl Then
        MsgBox "Ctrl & Clicked"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

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