+ Reply to Thread
Results 1 to 12 of 12

Adding shortcut keys to Custom menus

Hybrid View

  1. #1
    Registered User
    Join Date
    08-17-2010
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    19

    Adding shortcut keys to Custom menus

    Hi,

    I want help to add shortcut keys to all the custom menus/sub-menus in tool
    o For example, for the “Juniper Tool 1B” menu item, use “J” as the shortcut key.
    It's urgent, Can someone please help. I am relatively new to Excel programming.

  2. #2
    Registered User
    Join Date
    01-20-2010
    Location
    Dusseldorf, Germany
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Adding shortcut keys to Custom menus

    Hi, you can do it using the & sign before the letter you want to use as shortcut:

    SubAccessMenu.Caption = "&Access"
    This will generate the Menu name with A as shortcut.
    Visit the Advanced Features Add-In for MS 2003 Official Website or if you're looking for VBA help you can just surf the VBA Handbook

  3. #3
    Registered User
    Join Date
    08-17-2010
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Adding shortcut keys to Custom menus

    I have the code like this:
    Private Sub Workbook_Activate()
    Call CreateMenu
    End Sub
    So, I add this line, after the Call CreateMenu?
    Last edited by romperstomper; 08-17-2010 at 04:25 AM.

  4. #4
    Registered User
    Join Date
    04-16-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Adding shortcut keys to Custom menus

    Hi Gayathri,

    At run time you need to add this line of code. The object name for the sub menu (the one which is going to be customized) will be set at run time.

    Regards,
    Ram

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,980

    Re: Adding shortcut keys to Custom menus

    You need to alter your CreateMenu subroutine to change the captions it uses.
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Registered User
    Join Date
    08-17-2010
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Adding shortcut keys to Custom menus

    I see that there is a line which says MenuObject.Caption = Caption, based on the value passed on to the MenuObject. Now, if I want to underline the first letter of each of the value passed to Caption, how to I do that with the & symbol?

  7. #7
    Registered User
    Join Date
    08-17-2010
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Adding shortcut keys to Custom menus

    This is what I have presently

    Sub CreateMenu()
    '   This sub is executed when the workbook is opened, and creates a custom menu on the toolbar
    
    Dim XYZMenu As Worksheet
    Dim MenuObject As CommandBarPopup
    Dim MenuItem As Object
    Dim SubMenuItem As CommandBarButton
    Dim row As Integer
    Dim MenuLevel, NextLevel, PositionOrMacro, Caption, Divider, FaceId
    
    On Error GoTo ErrH
    
    
    '   Location for menu data
    Set XYZMenu = ThisWorkbook.Sheets("XYZMenu")
    
    '   Make sure the menus aren't duplicated
    Call DeleteMenu
        
    '   Initialize the row counter
    row = 2
    
    '   Add the menus, menu items and submenu items using data stored on the XYZMenu tab
        
    Do Until IsEmpty(XYZMenu.Cells(row, 1))
        With XYZMenu
            MenuLevel = .Cells(row, 1)
            Caption = .Cells(row, 2)
            PositionOrMacro = .Cells(row, 3)
            Divider = .Cells(row, 4)
            FaceId = .Cells(row, 5)
            NextLevel = .Cells(row + 1, 1)
        End With
        
        Select Case MenuLevel
            Case 1 ' A Menu
    '              Add the top-level menu to the Worksheet CommandBar
                Set MenuObject = Application.CommandBars(1). _
                    Controls.Add(Type:=msoControlPopup, _
                    Before:=PositionOrMacro, _
                    temporary:=True)
                MenuObject.Caption = Caption
            
            Case 2 ' A Menu Item
                If NextLevel = 3 Then
                    Set MenuItem = MenuObject.Controls.Add(Type:=msoControlPopup)
                Else
                    Set MenuItem = MenuObject.Controls.Add(Type:=msoControlButton)
                    MenuItem.OnAction = PositionOrMacro
                End If
                MenuItem.Caption = Caption
                If FaceId <> "" Then MenuItem.FaceId = FaceId
                If Divider Then MenuItem.BeginGroup = True
            
            Case 3 ' A SubMenu Item
                Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
                SubMenuItem.Caption = Caption
                SubMenuItem.OnAction = PositionOrMacro
                If FaceId <> "" Then SubMenuItem.FaceId = FaceId
                If Divider Then SubMenuItem.BeginGroup = True
        End Select
        row = row + 1
    Loop
    
    Worksheets("XYZMenu").Visible = xlVeryHidden
    
    
    Exit Sub
    ErrH:
    MsgBox "There was an error in creating the custom XYZ menu on your toolbar." & _
        Application.StatusBar = False
    Application.DisplayStatusBar = True
    End Sub
    
    Sub DeleteMenu()
    '   This sub is executed when the workbook is closed, and deletes the menu
    
    Dim XYZMenu As Worksheet
    Dim row As Integer
    Dim Caption As String
    
    On Error Resume Next
    Set XYZMenu = ThisWorkbook.Sheets("XYZMenu")
    row = 2
    Do Until IsEmpty(XYZMenu.Cells(row, 1))
        If XYZMenu.Cells(row, 1) = 1 Then
            Caption = XYZMenu.Cells(row, 2)
            Application.CommandBars(1).Controls(Caption).Delete
        End If
        row = row + 1
    Loop
    On Error GoTo 0
    End Sub
    Last edited by romperstomper; 08-17-2010 at 04:41 AM. Reason: add code tags

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,980

    Re: Adding shortcut keys to Custom menus

    Use:
    MenuItem.Caption = "&" & Caption
    and:
    SubMenuItem.Caption = "&" & Caption
    Note: you must use code tags when posting code, as specified in the forum rules. I have added them for you on this occasion but you must do it in future.

  9. #9
    Registered User
    Join Date
    08-17-2010
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Adding shortcut keys to Custom menus

    I apologize. Henceforth, I shall add them in future.
    Thanks for your response. Just a question to improve my understanding. If the first letter had to be chosen as shortcut key, an & was used before the Caption. If any other letter has to be "shortkeyed" then, how is that achieved?

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,980

    Re: Adding shortcut keys to Custom menus

    Well, the simplest way would be to leave the code as it was originally and add the & signs to the cells containing the captions. Otherwise you would have to parse the caption strings to work out where to put the & sign.

  11. #11
    Registered User
    Join Date
    08-17-2010
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Adding shortcut keys to Custom menus

    Thank you very much, and is there a way I can add short cut keys to buttons on the sheet also?

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,980

    Re: Adding shortcut keys to Custom menus

    If you are using buttons from the control toolbox, then set the Accelerator property to the letter you want. If you are using Forms controls, then you can assign a shortcut key via the macros dialog to the macro that is attached to the button. (even though the Button object has an Accelerator property, I don't know of any way to set it).

+ 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