首页 > Excel专区 > Excel函数 >

用VBA将焦点定位于名称框

Excel函数 2022-02-14 21:56:30

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


标签: 名称快捷键焦点excel函数

office教程网 Copyright © 2016-2020 http://www.office9.cn. Some Rights Reserved. 苏ICP备20040415号-9