Simulate F2 programmatically

danitouffaha

New Member
Joined
Nov 16, 2012
Messages
18
Hi,

I have a need to disable F2 for certain cells based on a specific condition.
I have added this below to call customF2Fct when F2 is pressed
VBA Code:
Application.OnKey "{F2}", "customF2Fct"

Now in customF2Fct

VBA Code:
Public Function customF2Fct()
    Selection.Activate
    'continue as F2 if the condition is met
    If Condition = True Then
        Application.SendKeys Keys:="{F2}"
    Else
        Application.SendKeys Keys:="{Esc}"
    End If
    DoEvents
End Function

The escape works and I avoid the cell being open for editing, but if the condition is true, the sendKeys F2 does not open the cell for editing.
Instead it toggles the numLock.
Can you please help with what I am missing?

Thanks
Dani
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Can you not edit the cell if the condition is met?
 
Upvote 0
Can you not edit the cell if the condition is met?
I am not sure I understand!
I can edit the cell if I start typing, but the cursor is not inside the cell at the end as usual.
if the condition is met, then pressing F2 should act like pressing F2 originally before my code.
If the condition is not met, then you will not have the cursor in the cell.
Please let me know if it is clearer now
Thanks
 
Upvote 0
no that didnt work, it is still toggling the numLock, no idea why??
It's working for me...Have you tried removing the Parenthesis? Shooting in the dark here....
VBA Code:
Application.SendKeys "{F2}{END}"
 
Upvote 0
It's working for me...Have you tried removing the Parenthesis? Shooting in the dark here....
VBA Code:
Application.SendKeys "{F2}{END}"
I know it should work and its straight forward, there has to be something wrong with the workbook,
I tried if there is any other event triggered after but there is nothing
I will close it and reboot and will let you know if it will work.
Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,266
Messages
6,123,962
Members
449,137
Latest member
yeti1016

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