excelperfect
如果你不想使用Excel內(nèi)置的快捷鍵,或者不希望你開發(fā)的界面因為快捷鍵而被破壞,你可能會想禁用所有的Excel快捷鍵。
下面的程序禁用所有的Excel快捷鍵:
Sub Disable_Keys()
Dim StartKeyCombination As Variant
Dim KeysArray As Variant
Dim Key As Variant
Dim I As Long
On Error Resume Next
'Shift鍵 = '+' (加號)
'Ctrl鍵 = '^' (脫字號)
'Alt鍵 = '%' (百分號)
'使用這些鍵及其組合填充數(shù)組
'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}')
'禁用StartKeyCombination中每個鍵與KeysArray中的組合鍵
For Each Key In KeysArray
Application.OnKey StartKeyCombination & Key, ''
Next Key
'禁用StartKeyCombination中每個鍵與其它鍵的組合鍵
For I= 0 To 255
Application.OnKey StartKeyCombination & Chr$(I), ''
Next I
'禁用F1 -F15鍵與Shift, Ctrl 或 Alt 鍵的組合鍵
For I= 1 To 15
Application.OnKey StartKeyCombination & '{F' & I &'}', ''
Next I
Next StartKeyCombination
'禁用F1 - F15 鍵
For I = 1 To 15
Application.OnKey '{F' & I & '}',''
Next I
'禁用PGDN和PGUP鍵
Application.OnKey '{PGDN}', ''
Application.OnKey '{PGUP}', ''
End Sub
下面的程序啟用所有的Excel快捷鍵:
Sub Enable_Keys()
Dim StartKeyCombination As Variant
Dim KeysArray As Variant
Dim Key As Variant
Dim I As Long
On Error Resume Next
'Shift鍵 = '+' (加號)
'Ctrl鍵 = '^' (脫字號)
'Alt鍵 = '%' (百分號)
'使用這些鍵及其組合填充數(shù)組
'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}')
'啟用StartKeyCombination中每個鍵與KeysArray中的組合鍵
For Each Key In KeysArray
Application.OnKey StartKeyCombination & Key
Next Key
'啟用StartKeyCombination中每個鍵與其它鍵的組合鍵
For I= 0 To 255
Application.OnKey StartKeyCombination & Chr$(I)
Next I
'啟用F1 -F15鍵與Shift, Ctrl 或 Alt 鍵的組合鍵
For I= 1 To 15
Application.OnKey StartKeyCombination & '{F' & I &'}'
Next I
Next StartKeyCombination
'啟用 F1 - F15 鍵
For I = 1 To 15
Application.OnKey '{F' & I & '}'
Next I
'啟用PGDN和PGUP鍵
Application.OnKey '{PGDN}'
Application.OnKey '{PGUP}'
End Sub
你可以在工作簿打開時禁用所有快捷鍵,在關(guān)閉工作簿時恢復(fù)所有快捷鍵,這需要你在ThisWorkbook模塊中放置下面的代碼:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Enable_Keys
End Sub
Private Sub Workbook_Open()
Disable_Keys
End Sub
建議在這個工作簿開發(fā)時,在功能區(qū)或者工作表界面放置一個恢復(fù)所有快捷鍵的按鈕,單擊該按鈕可以恢復(fù)所有快捷功能,以避免在需要時不方便編輯代碼的情形。
聯(lián)系客服