Consulting

Results 1 to 4 of 4

Thread: Menu Keyboard Shortcuts

  1. #1
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location

    Menu Keyboard Shortcuts

    I have my own menus in Excel 2000, as can be seen in the example below.

    [VBA]Set myCommandBarCtl = myBar.Controls.Add(Type:=msoControlButton)
    With myCommandBarCtl
    .BeginGroup = True
    .Style = msoButtonCaption
    .Caption = "Oprette Link"
    .TooltipText = "Link til arkiv folder"
    .OnAction = "addlink"
    End With[/VBA]

    However a user has asked if it is possible to add a keyboard shortcut to run the code, instead of having to use the mouse to navigate.
    Is there a quick addition I can make to the code above to solve the problem, or will I have to create a new Macro for each shortcut?

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Ukdane,

    You can add a shortcut key combination to run a macro, but it wouldn't be added to the command bar building proedure. Rather, try adding akin to below, to the workbook activate and deactivate events. This way, its only in effect while the user has your workbook in use.

    See the help topic OnKey for different key assigning; the below assigns CTRL + "d" to your macro; and the OnKey w/no arg resets the combo to its default action.

    Hope this helps,

    Mark

    [vba]Private Sub Workbook_Activate()
    Application.OnKey "^d", "addlink"
    End Sub

    Private Sub Workbook_Deactivate()
    Application.OnKey "^d"
    End Sub[/vba]

  3. #3
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Cool. Thanks.
    Any idea how I can utilise the F1-F12 buttons, and F13-F25 (if we use SHIFT)?

    Cheers

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by ukdane
    Cool. Thanks.
    Any idea how I can utilise the F1-F12 buttons, and F13-F25 (if we use SHIFT)?

    Cheers
    Happy to be of assistance. For help on string construction, in Excel 2000, select the 'Index' tab in VBA Help and type the word "onkey" in the search box. The help topic will assist you in string construction, such as where to add french braces, etc...

    I found this particular help topic to be fairly clear, but if after reading the help file your code "jams", just include the code and we can take a 'looksee'.

    Mark

Posting Permissions

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