Set focus back to the application window after showing userform
When showing a userform (running its
Show method) it not only shows up on the screen but also takes the focus (the destination of e.g. keystrokes).
Say, the userform is a custom made toolbar. Its
Show fires 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.
Unfortunately, it seems
SetFocus method is not valid for application objects.
So how is this done?
I suppose the solution for my example comes after
Private Sub Workbook_Open() [...] UserForm1.Show
i use this one :
this resets the focus from a userform to your Excel Sheet.
How To Set Focus To The Excel Window After Showing A Userform , Learn how to use VBA to move the focus back to your spreadsheet from a userform. Set focus to Excel window after showing userform. When a user click on a certain cell in my workbook, I show a form right next to the cell. The form is not shown modal, so the user can continue moving around the spreadsheet. As soon as the form is shown, it receives focus.
right after the
Show statement seems to work fine.
In other cases
AppActivate "Microsoft Excel"
may also be ok.
Set focus to Excel window after showing userform, Set focus to Excel window after showing userform You could also set the focus back to the cell the user just clicked on probably Activating the window or a cell doesn't set the focus to the main Excel Application window. There may be situations when you are using Excel Userforms where you would like the focus of the computer to remain or be placed on the Excel spreadsheet and not the userform itself. This will typically be in instances where you are using a modeless (floating) userform .
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() . . . . . . . . . . Application.OnTime Now(), "MoveFocusToWorksheet" End Sub
In any of the general code modules (add one if you have none), declare an API function:
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:
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
AppActivate statement (VBA), Its Show fires 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 In addition, since showing form front with windows API I shared in my previous replies works for the other document and that showdialog with a parameter just sets the container, I don't think that will make sense, since that document isn't a solid container, and even I set it to the instance of that document, that will not work for it.
An other form is:
Mastering MIcrosoft VBA, When showing a userform (running its Show method) it not only shows up on the screen but also takes the focus (the destination of e.g. keystrokes). Say, the Is there a way to keep the focus on the userform or at least regain the focus from the worksheet. This thread is locked. You can follow the question or vote as helpful, but you cannot reply to this thread.
I use AppActivate ActiveWindow.Caption because AppActivate Application.Caption can focus the wrong window if multiple windows are opened for the same workbook.
SetFocus For VBA UserForms, Creating a Modeless Dialog Box VBA version 6 and later let you can create a When you display a modeless dialog box, it takes the focus just as any modal dialog can click in the application window to transfer the focus back to that window. as simple as setting the ShowModal property of the user form to False from its Set focus back to the application window after showing userform i use this one : AppActivate Application.caption this resets the focus from a userform to your Excel Sheet. excel vba excel-vba focus userform
Mastering VBA for Microsoft Office 2016, When you show a userform, either modally or modelessly, the input focus is on that You can accomplish this with a few simple Windows API calls. In the userform's code module, paste the following API function declarations at the top This will cause the form to set focus back to the worksheet only if the Back to your original question, you use Application.ShowModelessDialog(), instead of Form.Show() to show a flowing form the first time, which makes the amin AutoCAD window own the floating form, so that it would not fall behind the AutoCAD main window.
Mastering VBA for Microsoft Office 2013, could display two graphics illustrating a choice of two formats for a document. We're using VBA version 7.1, and ever since version 6 the language has But you can click in the application window to transfer the focus back to that window. is as simple as setting the ShowModal property of the user form to False from its I can put a CommandButton on the userform with Userform16.Textbox1.Setfocus and it works as expected to move the focus to Textbox1. I set up a simple test event (see below) to move the textbox focus back up to TextBox1 when Textbox2 is entered. It moves focus to Textbox3 when I tab out of TextBox1.
For example, you could display two graphics illustrating a choice of two formats for We're using VBA version 7, and ever since version 6 the language has But you can click in the application window to transfer the focus back to that window. dialog box is as simple as setting the ShowMooIal property of the user form to Other properties can be set only when a control doesn't have the focus. For example, you can't set a control's Visible or Enabled properties to False (0) when that control has the focus. You can also use the SetFocus method to navigate in a form according to certain conditions.