我有一个简短的宏,可以使用以下命令将单元格置于编辑模式:
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.
我想在宏中包含一个测试来检查这一点:
如果工作表是活动窗口,则继续运行。
如果 VBE 是活动窗口,则发出警告 MsgBox 并中止。
我不知道如何确定哪个窗口处于活动状态。ActiveWindow.Caption
总是显示类似:
Book1.xlsm
我认为您不幸遇到了 VBE 问题。如果解决方案是避免编码时犯错误,这对您有用吗?似乎没有办法查看 activewindow 是否是我能找到的 VBE。如果这个解决方案不能满足您的需求,在 WHS 中使用 VBS https://stackoverflow.com/questions/8356046/cycle-through-open-windows-applications可能适合您或调用像 autoit 这样的外部程序。
请注意,如果您单步执行代码,它将不起作用,但我假设您实际上主要关心的是从 VBE 窗口中点击运行按钮,并且它将起作用。将第一行放在代码的顶部,或者放在包含 sendkeys 或其他内容的部分之前。
ThisWorkbook.VBProject.VBE.MainWindow.Visible = False
If ActiveWindow.Caption <> "Book1.xlsm" Then MsgBox ("Test") Else MsgBox ("testelse")
您将无法按 alt-tab 键返回,必须按 Alt-F11
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)