MsgBox在Excel中的焦点 [英] MsgBox focus in Excel

查看:86
本文介绍了MsgBox在Excel中的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Excel中使用VBA计算大量数据,并希望在完成时显示MsgBox. MsgBox实际上显示了计算所需的时间.

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.

问题是当用户在计算发生时决定做其他事情时. Excel继续计算,完成后,MsgBox会显示,但由于某些原因,Excel不会将焦点放在MsgBox上. Excel图标将在任务栏中闪烁,如果单击它,Excel会最大化,但是MsgBox在Excel窗口的后面,我们永远不能单击它.因此,摆脱这种情况的唯一方法是对excel.exe进行杀戮……不是很好. Alt + Pause也不起作用,因为该代码仅在当前代码行之后才停止,直到MsgBox关闭时该行才结束.

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.

我之前没有成功尝试过功能AppActivate("Microsoft Excel")(如何将焦点放在msgbox上?).该应用程序名称实际上比自Excel 2010将文档名称添加到窗口标题以来要长.

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?

推荐答案

我尝试了所有其他大多数答案:

I tried most of all the other answers:

  • ThisWorkbook.Activate
  • AppActivate()
  • Application.Wait()
  • Sleep
  • ThisWorkbook.Activate
  • AppActivate()
  • Application.Wait()
  • Sleep library

无论出于何种原因,以上都不起作用.我相信我们的计算机环境中确实存在一些特定的业务设置,可能会造成问题.就像其他人提到的那样,以上所有解决方案都可能适用于安装了任何版本Office 2010的任何全新格式的Windows 7安装,即使是在双显示器安装中也是如此.因此,请为所有这些答案+1.另外,我注意到有问题的仅发生在某些特定的工作簿中.确实很奇怪,这可能只是与我在工作簿中所做的事情(无论是VBA还是简单的Excel)有关的Office 2010错误.

For whatever reason, none of the above works. I believe there is some really specific business setting in our computer environment that could be creating the problem. As others mentionned, all the above solutions should probably work on any brand new formatted Windows 7 install with any version of Office 2010 installed, even in a dual-monitor setup. So +1 to all of these answers. Also, I noticed the problematic only happens in some specific workbooks. Really weird behavior, that might just be an Office 2010 bug related to something I do in my workbook (whether it's VBA or simply Excel).

话虽如此,我真正的解决方案(实际上并不是最初问题的解决方案)没有使用MsgBox().确实存在一些变通办法,我发现Forms对我而言是最好的.因此,我没有在这个问题上浪费更多的时间,而是给出了非常简单的以下代码来替换原始的MsgBox():

With that being said, my real solution (which isn't really a solution to the initial problem) is not using MsgBox(). A couple of workarounds do exist and I found out that Forms are the best one for me. So instead of wasting more time in this problem, I came out with the really simple following code to replace my original MsgBox():

Application.ScreenUpdating = False
frmMsgBox.Show
Application.ScreenUpdating = True

我可以将所需的任何文本放在frmMsgBox的标签中,由于这是Excel,因此我可以使用隐藏的单元格简单地传递参数.

I can put whatever text I want in a label in frmMsgBox, and since this is Excel, I can simply pass parameters by using hidden cells.

感谢所有帮助.

这篇关于MsgBox在Excel中的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆