Có thể tạo phím tắt bắt đầu bằng "ALT+..." được không

Liên hệ QC

AnhThu-1976

Thành viên tích cực
Tham gia
17/10/14
Bài viết
1,061
Được thích
175
Cho em hỏi tạo phím tắt bắt đầu ALT +... được không?
Ví dụ: em có code là Test2
Và em muốn tạo phím tắt là ALT+i
Cách em làm như sau:
Code trong Thisworkbook là
Private Sub Workbook_Activate()
Application.OnKey "%{I}", "Test2"
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "%{I}", ""
End Sub
Nhưng nó không chạy, nhờ anh chị hướng dẫn, em cảm ơn!
 
Bạn đụng vào mấy ký tự của hệ thống nên nó không chạy thôi.
Tránh mấy chữ này: ATL + (A, D, E, H, I, O, Q, R, T, V, W)
 
Upvote 0
Cũng đã thay đổi nhưng nó vẫn không chạy, cụ thể ALT+N hay ALT+M
Bác thử dùng tắt phím tắt window trược khi chạy lệnh xem được không? em chưa có thử

Mã:
Sub Off_Keys()
    Dim StartKeyCombination As Variant
    Dim KeysArray As Variant
    Dim Key As Variant
    Dim I As Long

    On Error Resume Next

    'Shift key = "+"  (plus sign)
    'Ctrl key = "^"   (caret)
    'Alt key = "%"    (percent sign
    'We fill the array with this keys and the key combinations
    'Shift-Ctrl, Shift- Alt, Ctrl-Alt, Shift-Ctrl-Alt

    For Each StartKeyCombination In Array("+", "^", "%", "+^", "+%", "^%", "+^%")

        KeysArray = Array("{BS}", "{BREAK}", "{CAPSLOCK}", "{CLEAR}", "{DEL}", _
                    "{DOWN}", "{END}", "{ENTER}", "~", "{ESC}", "{HELP}", "{HOME}", _
                    "{INSERT}", "{LEFT}", "{NUMLOCK}", "{PGDN}", "{PGUP}", _
                    "{RETURN}", "{RIGHT}", "{SCROLLLOCK}", "{TAB}", "{UP}")

        'Disable the StartKeyCombination key(s) with every key in the KeysArray
        For Each Key In KeysArray
            Application.OnKey StartKeyCombination & Key, ""
        Next Key

        'Disable the StartKeyCombination key(s) with every other key
        For I = 0 To 255
            Application.OnKey StartKeyCombination & Chr$(I), ""
        Next I

        'Disable the F1 - F15 keys in combination with the Shift, Ctrl or Alt key
        For I = 1 To 15
            Application.OnKey StartKeyCombination & "{F" & I & "}", ""
        Next I

    Next StartKeyCombination


    'Disable the F1 - F15 keys
    For I = 1 To 15
        Application.OnKey "{F" & I & "}", ""
    Next I

    'Disable the PGDN and PGUP keys
    Application.OnKey "{PGDN}", ""
    Application.OnKey "{PGUP}", ""
End Sub



Sub On_Keys()
    Dim StartKeyCombination As Variant
    Dim KeysArray As Variant
    Dim Key As Variant
    Dim I As Long

    On Error Resume Next

    'Shift key = "+"  (plus sign)
    'Ctrl key = "^"   (caret)
    'Alt key = "%"    (percent sign
    'We fill the array with this keys and the key combinations
    'Shift-Ctrl, Shift- Alt, Ctrl-Alt, Shift-Ctrl-Alt

    For Each StartKeyCombination In Array("+", "^", "%", "+^", "+%", "^%", "+^%")

        KeysArray = Array("{BS}", "{BREAK}", "{CAPSLOCK}", "{CLEAR}", "{DEL}", _
                    "{DOWN}", "{END}", "{ENTER}", "~", "{ESC}", "{HELP}", "{HOME}", _
                    "{INSERT}", "{LEFT}", "{NUMLOCK}", "{PGDN}", "{PGUP}", _
                    "{RETURN}", "{RIGHT}", "{SCROLLLOCK}", "{TAB}", "{UP}")

        'Enable the StartKeyCombination key(s) with every key in the KeysArray
        For Each Key In KeysArray
            Application.OnKey StartKeyCombination & Key
        Next Key

        'Enable the StartKeyCombination key(s) with every other key
        For I = 0 To 255
            Application.OnKey StartKeyCombination & Chr$(I)
        Next I

        'Enable the F1 - F15 keys in combination with the Shift, Ctrl or Alt key
        For I = 1 To 15
            Application.OnKey StartKeyCombination & "{F" & I & "}"
        Next I

    Next StartKeyCombination


    'Enable the F1 - F15 keys
    For I = 1 To 15
        Application.OnKey "{F" & I & "}"
    Next I

    'Enable the PGDN and PGUP keys
    Application.OnKey "{PGDN}"
    Application.OnKey "{PGUP}"
End Sub
 
Upvote 0
Cũng đã thay đổi nhưng nó vẫn không chạy, cụ thể ALT+N hay ALT+M

- Tôi không nhớ hết các Shortcut key đi với ALT, bạn phải bấm kiểm tra thử các phím trên nó có kích hoạt menu nào không rồi mới dùng.
Alt+N: là Insert
Alt+M: là Formula
- Bạn kiểm tra lại tầm vực của cái thủ tục (Sub) "test2" của bạn. Để gọi nó bạn phải lưu nó ở standard module chứ không phải Worksheet module.
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom