0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ワークシートでショートカット割当設定【Excel VBA】

Posted at

使い方

  • 「設定」というシートを用意し、下記のようなリストを用意する
キー1 キー2 キー3 動作
Ctrl E 例1
Ctrl Shift E 例2

コード

標準モジュール

Sub ショートカットキーの割り当て()
        Dim キー As String
        Dim i As Integer
        
    i = 4
    Do While Sheets("設定").Cells(i, 4) <> ""
        With Sheets("設定")
            キー = .Cells(i, 1).Value & .Cells(i, 2).Value & .Cells(i, 3).Value
            キー = Replace(キー, "Ctrl", "^")
            キー = Replace(キー, "Alt", "%")
            キー = Replace(キー, "Shift", "+")
            キー = Replace(キー, "Enter", "~")
            Application.OnKey キー, .Cells(i, 4).Value
            キー一覧 = キー一覧 & キー & ","
        End With
        i = i + 1
    Loop
    
    SaveSetting "MyMacro", "設定", "ショートカット登録分", キー一覧
    
End Sub

Sub ショートカットキーの解放()
    キーArray = Split(GetSetting("MyMacro", "設定", "ショートカット登録分"), ",")
        
    For i = 0 To UBound(キーArray) - 1
        Application.OnKey キーArray(i)
    Next i

End Sub

ThisWorkbookモジュール

アクティブ化、非アクティブ化した際に動作するようにします

Private Sub Workbook_Activate()
    Call ショートカットキーの割り当て
End Sub

Private Sub Workbook_Deactivate()
    Call ショートカットキーの解放
End Sub

シートモジュール

こちらもアクティブ化、非アクティブ化した際に動作するようにします

Private Sub Worksheet_Activate()
    Call ショートカットキーの割り当て
End Sub

Private Sub Worksheet_Deactivate()
    Call ショートカットキーの解放
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?