Consulting

Results 1 to 11 of 11

Thread: Activate Esc key in Calendar userform

  1. #1

    Activate Esc key in Calendar userform

    I've a calendar userform pop-up when user select cell range L60:L69.
    When user press Esc key, the calendar should disappear.
    I've the code like this.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Rng1            As Range
        'Assign the range to work with
        Set Rng1 = Range("L60:L69")
    'Only work on assigned range
        If Intersect(Target, Rng1) Is Nothing Then Exit Sub
        'Call the userform
        UserForm1.Show
    End Sub
    Please help

    Regards

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Put this in the code module for the Userform. Pressing the escape key will unload the userform.
    Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 27 Then Unload Me
    End Sub

  3. #3
    I've put it in code module of the user form but didn't have any effect

    Regards

  4. #4
    VBAX Mentor anandbohra's Avatar
    Joined
    May 2007
    Location
    Mumbai
    Posts
    313
    Location
    hi mikerickson

    the same happens with me when i try it to a blank form your code unload the user form
    but when i try it to form which has calender object

    no unload takes place pl help

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    It works on my machine, it sounds like a cross-platform issue. What happens when you put this in the userform's module and press ESC?
    Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    MsgBox KeyCode
    End Sub

  6. #6
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    put the code in the calendar key down event as the UF doesn't have focus
    Kind Regards,
    Ivan F Moala From the City of Sails

  7. #7
    VBAX Mentor anandbohra's Avatar
    Joined
    May 2007
    Location
    Mumbai
    Posts
    313
    Location
    Good Logic Ivan F Moala

    the default focus was on calender object that is why the form is not unloading

    Thanks mikerickson for the code & Thanks Ivan F Moala for giving hint for focus on calendar object.

  8. #8
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    I find in a user form it's helpful to have a "Cancel" command button, where Cancel is set to True - the user then knows there is a way to cancel out of the form, and can either click on the button or press Esc.

  9. #9
    hi mikerickson
    I've tried with both of your codes. But no effect.
    Based on Ivan's input, i modified the code as Private Sub calendar_KeyDown....
    But still pressing the Esc key do not have any effect?

    Please help.

    Regards
    Raj

  10. #10
    Hi,
    The calendar userform was working fine but I'm getting an error message now. Please help.
    I attached the screenshot.

    Bye

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •