哪个窗口是活动的 [英] Which Window Is Active
问题描述
Application.SendKeys{F2}
如果使用例如正常的工作表运行宏,则代码行正常运行功能区界面(这是因为 F2 将活动单元格放入编辑模式)
如果我运行宏的VBE 窗口,宏会失败,因为 VBE 将 F2 解释为启动对象浏览器的命令。
我想在宏中添加一个测试来检查:
如果工作表是活动窗口,请继续运行。
如果VBE是活动窗口,则发出警告MsgBox并中止。
我不知道如何确定哪个窗口处于活动状态。 ActiveWindow.Caption
总是显示如下:
Book1.xlsm
不幸的是,我觉得你遇到了VBE问题。如果解决方案是避免在编码时出错,这是否适合你?似乎没有办法看到activewindow是否可以找到VBE。如果此解决方案无法满足您的需求,在WHS中使用VBS 可能适用于您或者调用像autoit这样的外部程序。
请注意,如果您正在逐步执行代码,那么它将无法正常工作,但我认为您主要关心从VBE窗口中点击运行按钮,将为此工作。将第一行放在代码的顶部,或者在带有sendkeys或whatnot的部分之前。
ThisWorkbook.VBProject.VBE。 MainWindow.Visible = False
如果ActiveWindow.Caption<> Book1.xlsm然后MsgBox(Test)Else MsgBox(testelse)
你赢了不能替换,不得不击中Alt-F11
I have a short macro that places a cell in edit mode using the command:
Application.SendKeys "{F2}"
The line of code runs properly if the macro is run from the worksheet using, say, the normal Ribbon interface (that is because F2 puts the active cell into Edit Mode)
If I run the macro from the VBE window, the macro fails because the VBE interprets F2 to be a command to bring up the Object Browser.
I want to include a test in the macro to check for this:
If the worksheet is the active window, proceed running.
If the VBE is the active window, then issue a warning MsgBox and abort.
I don't know how to determine which window is active. ActiveWindow.Caption
always displays something like:
Book1.xlsm
I think you ran into a VBE issue unfortunately. If the solution is to avoid making mistakes while coding, will this work for you? It doesn't appear that there is a way to see if the activewindow is the VBE that I could find. If this solution won't meet your needs, Using VBS in WHS might work for you or calling an external program like autoit.
Note that it won't work if you are stepping through the code but I assume that you are really mainly concerned about hitting the run button from within the VBE window and it will work for that. Put the first line at the top of your code or right before the section with sendkeys or whatnot.
ThisWorkbook.VBProject.VBE.MainWindow.Visible = False
If ActiveWindow.Caption <> "Book1.xlsm" Then MsgBox ("Test") Else MsgBox ("testelse")
You won't be able to alt-tab back, have to hit Alt-F11
这篇关于哪个窗口是活动的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!