Set focus back to the application window after showing userform Set focus back to the application window after showing userform vba vba

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.


For me

AppActivate ThisWorkbook.Application

right after the Show statement seems to work fine.

In other cases

AppActivate "Microsoft Excel"

may also be ok.


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