Consulting

Results 1 to 8 of 8

Thread: Call macro by typing "Enter"

  1. #1
    VBAX Regular
    Joined
    Feb 2010
    Posts
    29
    Location

    Call macro by typing "Enter"

    in a sheet's codes:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Select Case Target.Address
            Case "$A$1": Application.OnKey "~", "Macro1"
            Case Else: Application.OnKey "~", "Macro2"
        End Select
    End Sub
    in a modul, there are macros

    Sub Macro1()
        'write your codes here
        MsgBox "Hello!"
    End Sub
    
    Sub Macro2()
        'write your codes here
        MsgBox ActiveCell.Address & ": " & "Hi ya!"
    End Sub
    now if Cells("A1") is chosen, <enter> we get "Hello", others get ActiveCell.Address and "Hi ya!"
    Attached Files Attached Files
    Last edited by Aussiebear; 01-19-2014 at 06:11 PM. Reason: wrapped the supplied code within the tags

  2. #2
    This is the help section... Where you usually ASK for help.
    So are you asking a question? Or are you giving other people information on how to do this?
    I don't understand.

  3. #3
    VBAX Regular
    Joined
    Feb 2010
    Posts
    29
    Location
    sorry about that. the question is: how can i use application.onkey once, no need typing another key to terminate onkey.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    how can i use application.onkey once, no need typing another key to terminate onkey.
    You can't.

    Try this:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Select Case Target.Address
        Case "$A$1": Application.OnKey "~", "Macro1"
        Case Else: Application.OnKey "~", "Macro2"
        End Select
    End Sub
    
    Sub Macro1()
        Application.OnKey "~"
        MsgBox "Hello!"
    End Sub
     
    Sub Macro2()
        Application.OnKey "~"
        MsgBox ActiveCell.Address & ": " & "Hi ya!"
    End Sub
    Or This:
    Sub Macro3()
        OnKeyClear_Tilde
        MsgBox "Hello!"
    End Sub
    
    Sub OnKeyClear_Tilde()
      Application.OnKey "~"
    End Sub
    
    Sub OnKeyClear_Tab()
      Application.OnKey "[TAB}"
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Regular
    Joined
    Feb 2010
    Posts
    29
    Location
    Sub OnKeyClear_Tab() Application.OnKey "[TAB}" End Sub
    there are so many sheets in my workbook, so i use one sheet as menu listing name of all other sheets.
    when choose and enter, the chosen sheet is selected. it works, but application.onkey still fires when i type {enter} at the other sheets. i want it automatic cancels after call marcro.

    now i use api for this and done. but i still like to use onkey, it seems simpler (?)

  6. #6
    i want it automatic cancels after call marcro.
    samt posted code above, to do just that

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You'd better state what you are trying to accomplish.

    Browsing to sheets can be done easily using the rightbutton when hovering in the left down corner of the Excel sheet.
    Another method would be creating hyperlinks in your 'summary' sheet.
    In neither case any VBA, let alone API, is required.

  8. #8
    VBAX Regular
    Joined
    Feb 2010
    Posts
    29
    Location
    thank you all

Posting Permissions

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