+ Reply to Thread
Results 1 to 8 of 8

OnKey or KeyDown withing Worksheet?

Hybrid View

  1. #1
    Registered User
    Join Date
    01-09-2007
    Posts
    54

    OnKey or KeyDown withing Worksheet?

    Hi,

    I have this code which puts a top border around specific cells.

     
    Worksheets("Sheet1").Activate
      'Can't select unless the sheet is active
    
    If ActiveCell.Value <> Selection.Offset(1, 0) Then
    
    ActiveCell.Borders.Item(xlEdgeBottom).Weight = xlMedium
    
    Selection.Offset(1, 0).Select
    
    Else: Selection.Offset(1, 0).Select
    End If

    It works fine when attached to a button but I want it to be assigned to either 'OnEnter' or 'Keydown' within the worksheet

    Any ideas?

    Thanks

    Alex

  2. #2
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    When manipulating Excel menus and keys, make sure to reinstate original function upon leaving (on desactivate event)
    Application.OnKey "{Enter}", yourmacro
    HTH
    Carim


    Top Excel Links

  3. #3
    Registered User
    Join Date
    01-09-2007
    Posts
    54
    How do you attach the code to the following ?

    Application.OnKey "{Enter}", yourmacro

  4. #4
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Sorry for not being clear enough ...
    yourmacro stands for the name you have to your macro ...

    1. Attaching is
    Application.OnKey "{Enter}", thenameofyourmacro

    2. Detaching is
    Application.OnKey "{Enter}"

  5. #5
    Registered User
    Join Date
    01-09-2007
    Posts
    54
    Thats great thanks, but when I put...

    Application.OnKey "{Enter}", "Macro1"

    Application.OnKey "{Enter}"


    The Macro doesn't work and pressing the {Enter} key simply moves the cursor down as normal?

  6. #6
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Again sorry ...
    I assumed you had already dealt with such an issue ...

    You need an event macro at the workbook level or at the worksheet level to enable and disable the onkey instruction ...
    Private Sub Worksheet_Activate()
    Application.OnKey "{Enter}", "Macro1"
    End Sub
    
    Private Sub Worksheet_Deactivate()
    Application.OnKey "{Enter}"
    End Sub
    Just in case, worksheet events are to be stored in the respective sheet module

+ 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