首页 > Excel专区 > Excel函数 >

用VBA将焦点定位于名称框

Excel函数 2023-01-13

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,就可以将焦点定位到名称框


Copyright © 2016-2023 office学习教程网 office.tqzw.net.cn. All Rights Reserved.