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.
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.
Hi, you can do it using the & sign before the letter you want to use as shortcut:
This will generate the Menu name with A as shortcut.![]()
SubAccessMenu.Caption = "&Access"
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
I have the code like this:
So, I add this line, after the Call CreateMenu?![]()
Private Sub Workbook_Activate() Call CreateMenu End Sub
Last edited by romperstomper; 08-17-2010 at 04:25 AM.
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
You need to alter your CreateMenu subroutine to change the captions it uses.
Everyone who confuses correlation and causation ends up dead.
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?
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
Use:
and:![]()
MenuItem.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.![]()
SubMenuItem.Caption = "&" & Caption
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?
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.
Thank you very much, and is there a way I can add short cut keys to buttons on the sheet also?
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).
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks