vba 显示用户表单后将焦点设置回应用程序窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28042521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set focus back to the application window after showing userform
提问by Greenberet
When showing a userform (running its Showmethod) it not only shows up on the screen but also takes the focus (the destination of e.g. keystrokes).
当显示一个用户表单(运行它的Show方法)时,它不仅显示在屏幕上,而且还获得焦点(例如击键的目的地)。
Say, the userform is a custom made toolbar. Its Showfires in Workbook_Open()but the form itself is used relatively rarely so we want the focus to go back to the main application window right after its appearance.
比如说,用户表单是一个定制的工具栏。它被Show触发,Workbook_Open()但表单本身相对很少使用,因此我们希望焦点在其出现后立即返回主应用程序窗口。
Unfortunately, it seems SetFocusmethod is not valid for application objects.
不幸的是,似乎SetFocus方法对应用程序对象无效。
So how is this done?
那么这是如何做到的呢?
I suppose the solution for my example comes after
我想我的例子的解决方案是在
Private Sub Workbook_Open()
[...]
UserForm1.Show
回答by Patrick Lepelletier
i use this one :
我用这个:
AppActivate Application.caption
AppActivate Application.caption
this resets the focus from a userform to your Excel Sheet.
这会将焦点从用户表单重置到您的 Excel 工作表。
回答by Greenberet
For me
为了我
AppActivate ThisWorkbook.Application
right after the Showstatement seems to work fine.
在Show声明之后似乎工作正常。
In other cases
在其他情况下
AppActivate "Microsoft Excel"
may also be ok.
也可以。
回答by Gene Skuratovsky
This is a bit tricky, but this is what can do.
这有点棘手,但这是可以做的。
In the subroutine “Private Sub UserForm_Initialize()”, add this as the last line:
在子程序“Private Sub UserForm_Initialize()”的最后一行添加:
Private Sub UserForm_Initialize()
. . . . . . . . . .
Application.OnTime Now(), "MoveFocusToWorksheet"
End Sub
In any of the general code modules (add one if you have none), declare an API function:
在任何通用代码模块中(如果没有,请添加一个),声明一个 API 函数:
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
In any of the general code modules (could be the one with the API declaration, of course), add this subroutine:
在任何通用代码模块(当然可以是带有 API 声明的模块)中,添加以下子例程:
Public Sub MoveFocusToWorksheet()
Dim Dummy As Long
ThisWorkbook.Worksheets("Sheet1").Activate
' "Sheet1" here is the tab name of the sheet you want to move focus to. _
Or simply use then: With shtABC.Activate _
where "shtABC" being the worksheet's CodeName, _
same as ThisWorkbook.Worksheets("Sheet1").CodeName, _
same as the sheets module name showing in the Project Explorer panel.
Dummy = SetForegroundWindow(Application.hwnd)
End Sub
回答by Yin Cognyto
Both AppActivate Application.Captionand (the better) AppActivate ActiveWindow.Captionmentioned in the other answers do their job in focusing back on the application window itself ... but they do NOTfocus on the actual cell/range where one typically wants the focus to be. For that, use:
无论AppActivate Application.Caption和(越好)AppActivate ActiveWindow.Caption在其他的答案中提到做他们的重点在应用程序窗口本身在后面的工作...但他们不注重实际的单元格/范围,其中一个典型的想重点是。为此,请使用:
ActiveCell.Activate
which has the benefit of not requiring an additional click on the cell area of the sheet where you want to return focus - an additional click that can potentially change the previous selection.
这样做的好处是不需要额外单击要返回焦点的工作表单元格区域 - 额外的单击可能会更改先前的选择。
回答by CETAB
I use AppActivate ActiveWindow.Caption because AppActivate Application.Caption can focus the wrong window if multiple windows are opened for the same workbook.
我使用 AppActivate ActiveWindow.Caption 是因为如果为同一个工作簿打开多个窗口,AppActivate Application.Caption 可能会聚焦错误的窗口。
回答by Riccardo La Marca
An other form is:
另一种形式是:
AppActivate ThisWorkbook.Name
回答by Geo Matrix
I created a floating menu with a user form and use this code to make my cursor leave the user form and jump/focus back to my worksheet. It works at the end of each command button code and with the initiation code of the user form as well.
我创建了一个带有用户表单的浮动菜单,并使用此代码使我的光标离开用户表单并跳转/聚焦回我的工作表。它在每个命令按钮代码的末尾以及用户表单的启动代码中起作用。
AppActivate ThisWorkbook.Application
AppActivate ThisWorkbook.Application
Just place the above line of code before the "End Sub" line of any command button code and the initial show userform code.
只需将上述代码行放在任何命令按钮代码和初始显示用户表单代码的“End Sub”行之前。
回答by CODE-REaD
As a footnote to this excellent discussion, in some circumstances you may avoid the focus problem by skipping the call to .Show, so the focus never moves in the first place. For example with Word, if you are updating a modeless form or dialog box, just update the required area and omit the call to .Show, e.g.:
作为这个精彩讨论的脚注,在某些情况下,您可以通过跳过对 的调用来避免焦点问题.Show,因此焦点从一开始就不会移动。例如对于Word,如果您要更新无模式表单或对话框,只需更新所需区域并省略对 的调用.Show,例如:
Sub ShowProblems(ByVal ProbLoc)
EditBox2.TextBox.Text = "Here is the problem location: " & ProbLoc
' not needed: EditBox2.Show vbModeless
End Sub
回答by Andrew J Martin
I create an object for the application e.g. Outlook, then change the WindowSate to Maximised (OlMaximized), then when I want to remove focus I minimise (olMinimized)
我为应用程序创建了一个对象,例如 Outlook,然后将 WindowSate 更改为最大化(OlMaximized),然后当我想移除焦点时我最小化(olMinimized)
Set OutlookObj = GetObject(, "Outlook.Application")
OutlookObj.ActiveExplorer.WindowState = olMinimized
OutlookObj.ActiveExplorer.WindowState = olMaximized
Or you change the state from inside the application, you can also change its location and size etc for more info see: https://msdn.microsoft.com/en-us/library/office/ff838577.aspx
或者您从应用程序内部更改状态,您还可以更改其位置和大小等以获取更多信息,请参阅:https: //msdn.microsoft.com/en-us/library/office/ff838577.aspx
Application.WindowState = xlMaximized
回答by Carlosmuerto
Private Sub UserForm_Activate()
RefRangeIn.SetFocus
End Sub
it work for me, in excel 2013 VBA
它对我有用,在 excel 2013 VBA 中

