Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA modeless form stops Function Keys

I'd like to have my own help file in my Excel program.

In the

Private Sub Workbook_Open()

I have

Application.OnKey "{F1}", "Help"

That works when I'm on the Excel sheet but my application is based on a fullscreen main userform which is displayed modeless.

When the userform is visible it blocks the F1 key somehow and the macro doesn't fire.

I thought that modeless forms didn't block code execution.

Any hints how can I make this work?

like image 241
Sphinx Avatar asked Jan 20 '17 15:01

Sphinx


1 Answers

You need to trap the keyDown event on the UserForm itself. When the UserForm has got the focus, whatever Key you press it goes to the UserForm.

'/UserForm1 is a sample name.

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
     If KeyCode = 112 Then '/ F1
        Call Help
     End If
End Sub
like image 196
cyboashu Avatar answered Nov 14 '22 15:11

cyboashu