Disabling All Function Keys Except One

Written by Allen Wyatt (last updated December 10, 2018)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


3

When someone is using Lorenzo's workbook, he would like only the F2 key to be accessible to the user. He wonders if there is a way to disable all the function keys in Excel, with the exception of the F2 key?

There is a way, but it must be done using macros. Specifically, you'll want to use the OnKey method of the Application object. This code will disable all the function keys with the exception of F2:

Private Sub Workbook_Open()
    Application.OnKey "{F1}", ""
    Application.OnKey "{F3}", ""
    Application.OnKey "{F4}", ""
    Application.OnKey "{F5}", ""
    Application.OnKey "{F6}", ""
    Application.OnKey "{F7}", ""
    Application.OnKey "{F8}", ""
    Application.OnKey "{F9}", ""
    Application.OnKey "{F10}", ""
    Application.OnKey "{F11}", ""
    Application.OnKey "{F12}", ""
End Sub

The macro must be added to the ThisWorkbook module and it will run automatically when the workbook is opened.

If desired, you could make the macro a bit shorter:

Private Sub Workbook_Open()
    Dim sTemp As String
    Dim J As Integer

    For J = 1 to 12
        If J <> 2 Then
            sTemp = "{F" & Trim(Str(J)) & "}"
            Application.OnKey sTemp, ""
        End If
    Next J
End Sub

You'll want to also make sure that when you close the workbook that you re-enable all the function keys. This macro, again, should be added to the ThisWorkbook module:

Private Sub Workbook_BeforeClose(CANCEL As Boolean)
    Application.OnKey "{F1}"
    Application.OnKey "{F3}"
    Application.OnKey "{F4}"
    Application.OnKey "{F5}"
    Application.OnKey "{F6}"
    Application.OnKey "{F7}"
    Application.OnKey "{F8}"
    Application.OnKey "{F9}"
    Application.OnKey "{F10}"
    Application.OnKey "{F11}"
    Application.OnKey "{F12}"
End Sub

You could also use a shorter version of the same code:

Private Sub Workbook_BeforeClose(CANCEL As Boolean)
    Dim sTemp As String
    Dim J As Integer

    For J = 1 to 12
        If J <> 2 Then
            sTemp = "{F" & Trim(Str(J)) & "}"
            Application.OnKey sTemp
        End If
    Next J
End Sub

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13523) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Updating Fields in Locked Forms

Updating form fields in Word can be confusing, especially when the fields are locked in a form. This tips explains why ...

Discover More

Removing HTML Tags from Text

HTML tags are great when you want to display information on a web page. They are not so great when you have them in a ...

Discover More

Determining If a Value is Out of Limits

Need to figure out if a value is outside of some arbitrary limit related to a different value? There are a number of ways ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

DOS from Macros

Need to run a DOS command from within one of your macros? The answer is the Shell command, described in this tip.

Discover More

Enforcing Moving Cells Up

When you design your worksheets, you probably want users to interact with those worksheets in specific ways. What ...

Discover More

Controlling Window Size when Opening Additional Workbooks

When you open multiple workbooks, the way in which Excel sizes them is not the best for your needs. This tip looks at a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2018-05-06 12:46:38

Willy Vanhaelen

@Bob
That is not correct. If you don't reset the keys, they stay disabled as long as the current session of Excel runs and they even are disabled in any other workbook(s) you opened or will open in that session. So it can be pretty annoying to use this because as long as the workbook containing these macros is not closed these function key are not available in ai all other open workbooks of that session.


2018-05-06 01:44:32

Bob

I found that it was unnecessary to reset the function keys on worksheet closure. It went back to defaults on closure because we were using worksheet macro.


2018-05-05 11:25:16

Willy Vanhaelen

Be aware that the macros in this tip only disable the function keys pressed alone.
Combinations with Ctrl, Alt and Shift are still possible (and there are a lot of them).


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.