Excel中提供了許多快捷鍵,但遺憾的是沒有提供一個快捷鍵可以將焦點定位與名稱框,下面的宏代碼就可以實現這個功能。
Public Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Sub SetFocusNameBox()
Dim Res As Long
Res = SetFocus( _
FindWindowEx( _
FindWindowEx( _
FindWindow("XLMAIN", Application.Caption) _
, 0, "EXCEL;", vbNullString) _
, 0, "combobox", vbNullString))
End Sub
我們可以給這個宏代碼指定一個快捷鍵,如Ctrl+Shift+N,方法如下:
單擊菜單“工具→宏→宏”,打開“宏”對話框,選擇“SetFocusNameBox”,單擊“選項”按鈕,在其中的文本框中輸入大寫的“N”,單擊確定。
以后,只要按快捷鍵Ctrl+Shift+N,就可以將焦點定位到名稱框