Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MsgBox focus in Excel

I am calculating a lot of data with VBA in Excel and want to show a MsgBox when it's done. The MsgBox actually shows the time it took for the calculation.

The problem is when the user decides to do something else while the computation happens. Excel continues to calculate, and when it's done, the MsgBox does show but for some reason, Excel doesn't give the focus to the MsgBox. The Excel icon will blink in the taskbar and if we click it, Excel does maximize, but the MsgBox is behind the Excel window and we can NEVER click it. So the only way to get out of it is to taskkill excel.exe... not really nice. Alt+Pause doesn't work either since the code will be stopped only after the current line of code, which ends... when the MsgBox is closed.

I tried the function AppActivate("Microsoft Excel") before without any success (How do I bring focus to a msgbox?). The application name is actually longer than that since Excel 2010 adds the document name to the window title.

Any idea how I could get around this annoying problem?

like image 612
dan Avatar asked Dec 04 '14 18:12

dan


People also ask

What does MsgBox in Excel mean?

In Excel VBA, you can use the MsgBox function to display a message box (as shown below): A MsgBox is nothing but a dialog box that you can use to inform your users by showing a custom message or get some basic inputs (such as Yes/No or OK/Cancel). While the MsgBox dialog box is displayed, your VBA code is halted.

What is MsgBox with example?

In an Access desktop database, the MsgBox Function displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked. Syntax. MsgBox ( prompt [, buttons ] [, title ] [, helpfile ] [, context ] )

What value does the MsgBox statement display?

MsgBox Function, MsgBox Statement Example The MsgBox function returns a value based on the button chosen by the user. The MsgBox statement uses that value to display a message that indicates which button was chosen. Style = 4 + 16 + 256 ' Define buttons.


2 Answers

This will work in Excel no matter which other application has focus:

Before the message box or any warning put the following code:

AppActivate Application.Caption
DoEvents

Trust me on this, this is amazing!

like image 91
DeerSpotter Avatar answered Sep 19 '22 05:09

DeerSpotter


I did some testing, and found a potential work around for you.

I set up this simple procedure to test your situation:

Sub test()
    If Application.Wait(Now + TimeValue("0:00:10")) Then
        MsgBox "Time expired"
    End If
End Sub

I run this, then minimize all windows, and when the timer is up, nothing happens. If I switch to Excel I can see the Message Box, but nothing otherwise.

So I tried this:

Sub test()
    If Application.Wait(Now + TimeValue("0:00:10")) Then
        ThisWorkbook.Activate
        MsgBox "Time expired"
    End If
End Sub

This time when I run the procedure, then minimize all windows, instead of seeing nothing the Message Box pops up (but not the Excel Window).

I think by adding ThisWorkbook.Activate right before your MsgBox code you can have the same happen in your file.

It doesn't quite get you all the way there, but hopefully is better than where you are at.

like image 20
guitarthrower Avatar answered Sep 19 '22 05:09

guitarthrower