Excel VBA - {ESC} key to close any open user forms

Posted on 2012-09-01
Last Modified: 2012-09-01
What is the code to close any open user forms with the {ESC} key?  Thanks.
Question by:AndresHernando

    Author Comment

    Clarification: When I hit the {esc} key, I want it to run code that will close any open user forms.
    LVL 44

    Accepted Solution

    Private Sub Worksheet_Activate()
    ' Goes to procedure CloseUserForms if escape key hit
    Application.OnKey "{ESC}", "CloseUserForms"
    End Sub

    Open in new window

    Put this in a Module.

    Sub CloseUserForms()
    End Sub

    Open in new window

    LVL 44

    Expert Comment

    by:Martin Liss
    The above assumes that you have one or more userforms opened modeless (Userform1.Show vbModeless) and that you press the escape key when a sheet has focus.
    LVL 48

    Assisted Solution

    You can use this:

    Sub CloseAll()
    Dim f As UserForm
        For Each f In UserForms
           Unload f
    End Sub

    Open in new window

    LVL 44

    Expert Comment

    by:Martin Liss
    This is better since a sheet does not have to be in focus. Note: use paulino's code to actually close the forms since it's more flexible.

    Option Explicit
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Const VK_ESCAPE = &H1B
    Private Sub Worksheet_Activate()
        Application.OnKey "{ESC}", ""
            x = GetAsyncKeyState(VK_ESCAPE)
            If GetAsyncKeyState(VK_ESCAPE) <> 0 Then
            End If
    End Sub

    Open in new window


    Author Closing Comment

    Thanks guys.  Both inputs are very helpful.  --Andres

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
    Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
    This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
    This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now