Assign keyboard shortcut to run procedure

前端 未结 9 2041
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 04:35

When I press F5 in the VBA editor I would always like to run my \"Sub Skynet()\" procedure. Is there any way to assign a keyboard shortcut to this procedure.

相关标签:
9条回答
  • 2020-11-29 05:02

    According to Microsoft's documentation

    1. On the Tools menu, point to Macro, and then click Macros.

    2. In the Macro name box, enter the name of the macro you want to assign to a keyboard shortcut key.

    3. Click Options.

    4. If you want to run the macro by pressing a keyboard shortcut key, enter a letter in the Shortcut key box. You can use CTRL+ letter (for lowercase letters) or CTRL+SHIFT+ letter (for uppercase letters), where letter is any letter key on the keyboard. The shortcut key cannot use a number or special character, such as @ or #.

      Note: The shortcut key will override any equivalent default Microsoft Excel shortcut keys while the workbook that contains the macro is open.

    5. If you want to include a description of the macro, type it in the Description box.

    6. Click OK.

    7. Click Cancel.

    0 讨论(0)
  • 2020-11-29 05:04

    For assigning a keyboard key to button on the sheet you can use this code, just copy this code to the sheet which contain the button.

    Here Return specifies the key and get_detail is the procedure name.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Application.OnKey "{RETURN}", "get_detail"
    
    End Sub
    

    Now within this sheet whenever you press Enter button the assigned macro will be called.

    0 讨论(0)
  • 2020-11-29 05:04

    F function keys (F1,F2,F3,F4,F5 etc.) can be assigned to macros with the following codes :

    Sub A_1()
        Call sndPlaySound32(ThisWorkbook.Path & "\a1.wav", 0)
    End Sub
    
    Sub B_1()
        Call sndPlaySound32(ThisWorkbook.Path & "\b1.wav", 0)
    End Sub
    
    Sub C_1()
        Call sndPlaySound32(ThisWorkbook.Path & "\c1.wav", 0)
    End Sub
    
    Sub D_1()
        Call sndPlaySound32(ThisWorkbook.Path & "\d1.wav", 0)
    End Sub
    
    Sub E_1()
        Call sndPlaySound32(ThisWorkbook.Path & "\e1.wav", 0)
    End Sub
    
    
    Sub auto_open()
        Application.OnKey "{F1}", "A_1"
        Application.OnKey "{F2}", "B_1"
        Application.OnKey "{F3}", "C_1"
        Application.OnKey "{F4}", "D_1"
        Application.OnKey "{F5}", "E_1"
    End Sub
    
    0 讨论(0)
  • 2020-11-29 05:04

    You can add some ALT+Letter shortcuts to the VBA editor environment. I added ALT+C to Comment selected text lines and A+X to Uncomment selected text lines:

    1. In the VBE, right-click on a toolbar and select Customize
    2. Optionally create a new toolbar
    3. Select the Command Tab and 'Edit' in the Categories listbox. Find 'Comment Block' in the Commands listbox. Click and drag it to the target toolbar. Left-click on it to open the context popup menu and select 'Image and Text'. Replace the name with &C.
    4. Repeat step 3 for the Uncomment Block command and replace the name with &X.
    5. Close the Customize popup window.

    There are built-in Alt+Letter commands that cannot be used for new shortcuts using letters: A,D,E,D,H,I,O,Q,R,T,V,W.

    0 讨论(0)
  • 2020-11-29 05:06

    F5 is a standard shortcut to run a macro in VBA editor. I don't think you can add a shortcut key in editor itself. If you want to run the macro from excel, you can assign a shortcut from there.

    In excel press alt+F8 to open macro dialog box. select the macro for which you want to assign shortcut key and click options. there you can assign a shortcut to the macro.

    0 讨论(0)
  • 2020-11-29 05:07

    Write a vba proc like:

    Sub E_1()
        Call sndPlaySound32(ThisWorkbook.Path & "\e1.wav", 0)
        Range("AG" & (ActiveCell.Row)).Select 'go to column AG in the same row
    End Sub
    

    then go to developer tab, macros, select the macro, click options, then add a shortcut letter or button.

    0 讨论(0)
提交回复
热议问题