+ Reply to Thread
Results 1 to 3 of 3

keydown event

  1. #1
    bfa
    Guest

    keydown event

    Hello

    Im trying to catch a keydown event on a worksheet. If i press on my
    worksheet any key it should response like an event. How can I make it?
    I found a lot of examples with controls, but no one on a worksheet.

    thnx

  2. #2
    K Dales
    Guest

    RE: keydown event

    The Excel Application has an OnKey method (not event procedure) that will run
    code when a particular key is pressed:
    Application.OnKey Key, Procedure
    Key is the key to look for, Procedure is the name of the procedure you want
    to run. Special keys such as TAB are designated as {TAB} (there are some
    exceptions - see help for details).

    But note: This is an Application setting, so it affects any workbooks you
    may have open in your Excel session, it overrides any normal Excel processing
    of the keys, and it stays in effect until it is deactivated (by omitting
    Procedure from the parameters). You need to be careful how you use it!

    "bfa" wrote:

    > Hello
    >
    > Im trying to catch a keydown event on a worksheet. If i press on my
    > worksheet any key it should response like an event. How can I make it?
    > I found a lot of examples with controls, but no one on a worksheet.
    >
    > thnx


  3. #3
    Peter T
    Guest

    Re: keydown event

    If you only need your key event in a one Workbook AND it's OK to delete or
    to change the value of the cursor cell, maybe something like this:

    Put the addition of cells in columns 2 & 3, of the row of the keypress, into
    the cell that "p" was entered:

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim nRow As Long, d As Double
    On Error GoTo errH
    If Target.Count = 1 And Target(1).Column > 3 Then
    If LCase(Target.Value) = ("p") Then
    Application.EnableEvents = False
    nRow = Target.Row
    d = Cells(nRow, 2).Value + Cells(nRow, 3)
    If d Then
    Target.Value = d
    Else: Target.Value = ""
    End If
    End If
    End If

    errH:
    Application.EnableEvents = True
    End Sub

    This Worksheet_Change event goes in a Sheet module (right-click sheet tab >
    View code).
    You could adapt to call your own macro:

    Target.Value = "" ' delete the key press with Events disabled
    MyMacro

    If you want trap the key on all worksheets, change Worksheet_Change to
    Workbook_SheetChange and put the code in the ThisWorkbook module.

    Regards,
    Peter T

    "bfa" <[email protected]> wrote in message
    news:[email protected]...
    > Hello
    >
    > Im trying to catch a keydown event on a worksheet. If i press on my
    > worksheet any key it should response like an event. How can I make it?
    > I found a lot of examples with controls, but no one on a worksheet.
    >
    > thnx




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1