目的
ショートカットキーもりもりのブック(特に他人がつくったもの)のショートカットをヘルプ的にパッと呼び出したい。
そのブックに仕込めれば仕込むもよし、アドインで別持ちしておくもよし。
VBE内を巡回してショートカットを割り当ててるところを読み込みにいくようなことも上級者ならできるんだろうけど、できないのでやってない。我こそはという猛者によるアップグレード版待ってます。
概要
- Ctrl+Shft+?でユーザーフォームを表示
- すでに表示中にCtrl+Shft+?を押すとユーザーフォームを閉じる
- 表示位置は右下(デフォルトだと左上とか中央になるけどまあ邪魔なんで)
- アイテム数が多いとアイテム数に合わせて縦に長くなるのでお好みで上限を設けてスクロールして
以下作り方
ユーザーフォーム
オブジェクト名は以下の通り
- uf_ショートカットキー一覧表示_
- lv_ショートカットキー一覧
- CommandButton1
ユーザーフォーム表示中にExcelを普通に操作できるようにしておく
コード
ThisWorkbook
Private Sub Workbook_Open()
Application.OnKey "^+?", "ShowUF_ショートカットキー一覧表示"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^+?"
End Sub
標準モジュール
Option Explicit
Private Declare PtrSafe Function SetFocus Lib "user32" (ByVal hwnd As LongLong) As LongLong
Sub ShowUF_ショートカットキー一覧表示()
With uf_ショートカットキー一覧表示_
If .Visible = False Then
.Show
Else
' 表示状態なら閉じる
Unload uf_ショートカットキー一覧表示_
End If
End With
SetFocus app.hwnd
End Sub
Function app() As Application
Set app = Application
End Function
ユーザーフォーム
Const h_ListItem行の高さ = 15
は要調整かも。
sb_ListView列幅設定
内の各列幅の値も要調整。
Option Explicit
Const h_ListItem行の高さ = 15
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Call sb_ListView初期化(lv_ショートカットキー一覧)
Call sb_ListViewアイテム登録(lv_ショートカットキー一覧)
Call sb_lvサイズ調整(lv_ショートカットキー一覧)
Call sb_UFサイズ調整
Call sb_UF初期位置を右下に
End Sub
Private Sub sb_ListViewアイテム登録(myLV As ListView)
With myLV.ListItems.Add: .Text = "Ctrl+": .SubItems(1) = "Q": .SubItems(2) = "機能1": End With
With myLV.ListItems.Add: .Text = "Ctrl+Shift+": .SubItems(1) = "Q": .SubItems(2) = "機能2": End With
With myLV.ListItems.Add: .Text = "Ctrl+Alt+Shift+": .SubItems(1) = "Q": .SubItems(2) = "機能3": End With
With myLV.ListItems.Add: .Text = "Ctrl+": .SubItems(1) = "W": .SubItems(2) = "機能4": End With
With myLV.ListItems.Add: .Text = "Ctrl+Alt+Shift+": .SubItems(1) = "P": .SubItems(2) = "機能5": End With
With myLV.ListItems.Add: .Text = "Ctrl+Alt+Shift+": .SubItems(1) = "A": .SubItems(2) = "機能6": End With
With myLV.ListItems.Add: .Text = "Ctrl+Alt+Shift+": .SubItems(1) = "D": .SubItems(2) = "機能7": End With
With myLV.ListItems.Add: .Text = "Ctrl+Alt+Shift+": .SubItems(1) = "X": .SubItems(2) = "機能8": End With
With myLV.ListItems.Add: .Text = "Ctrl+Alt+Shift+": .SubItems(1) = "F": .SubItems(2) = "機能9": End With
With myLV.ListItems.Add: .Text = "Ctrl+Alt+Shift+": .SubItems(1) = "K": .SubItems(2) = "機能10": End With
End Sub
Private Sub sb_ListView初期化(myLV As ListView)
With myLV
.View = lvwReport
.LabelEdit = lvwManual
.HideSelection = False
.AllowColumnReorder = True
.FullRowSelect = True
.Gridlines = True
.ColumnHeaders.Add , "_key1", "Key1"
.ColumnHeaders.Add , "_key2", "Key2"
.ColumnHeaders.Add , "_func", "機能"
End With
End Sub
Private Function sb_ListView列幅設定(myLV As ListView) As Long
Const c1 = 100
Const c2 = 30
Const c3 = 180
With myLV
.ColumnHeaders(1).Width = c1
.ColumnHeaders(2).Width = c2
.ColumnHeaders(3).Width = c3
End With
sb_ListView列幅設定 = c1 + c2 + c3
End Function
Private Sub sb_UF初期位置を右下に()
With Me
.StartUpPosition = 0
.Top = app.Top + app.Height - .Height - 4
.Left = app.Left + app.Width - .Width - 4
End With
End Sub
Private Sub sb_UFサイズ調整()
Me.Width = lv_ショートカットキー一覧.Width + 10
Me.Height = lv_ショートカットキー一覧.Height + 15
End Sub
Private Sub sb_lvサイズ調整(myLV As ListView)
With myLV
.Width = sb_ListView列幅設定(lv_ショートカットキー一覧)
.Height = .ListItems.Count * h_ListItem行の高さ
End With
End Sub
コード作成補助(ワークシート関数)
sb_ListViewアイテム登録
内を書いていくのがまあ面倒なんでワークシート関数で文字列結合とか接頭語とかいろいろサポートしてもらう。Excelは表計算ソフトであると同時にテキストエディターでもあります。
列1~3をコピーしてVBEにペーストすればコードができてる。
列1
=" With myLV.ListItems.Add: .Text = "&""""&[@key1]&""""
列2
=": .SubItems(1) ="&""""&[@key2]&""""
列3
=": .SubItems(2) ="&""""&[@function]&""""&": End With"