Home » excel » How to bring excel VBA UI to front

How to bring excel VBA UI to front

Posted by: admin May 14, 2020 Leave a comment

Questions:

My Codes

I have the following codes whereby it opens a file browser when click a button on UI

Sub GetOpenFile()
    Dim fileStr As String

    On Error GoTo Error

    fileStr = Application.GetOpenFilename()
    If fileStr = "False" Then Exit Sub
    Workbooks.Open fileStr

    Exit Sub

    Error:
    MsgBox "Something went wrong or the file type/extension is wrong", vbExclamation, "Error"

    Exit Sub
End Sub

My Problem

After selected the file it is able to open the file. But the problem is, UI window goes to background whereby the newly opened file came to front. User always needs to either minimize or look for the UI in order to proceed with other activities.

What I need

Is there any way that UI will stay in front even after opened the file?

How to&Answers:

I am able to solve it by doing the following way. After opened the file, I stored the opened file name in a variable as below

mainFileName = ActiveWorkbook.Name

After that just minimized the window by using following codes

Windows(mainFileName).WindowState = xlMinimized