如何切换到工作表?
我已经在MSDN中提出了这个问题
如何在运行时从Visual Basic编辑器切换到工作表?
如果程序没有运行,我可以点击查看Microsoft Excelbutton(Alt + F11)。 是否有一个VBA声明做同样的事情?
谢谢,谢恩。
Excel 2010 SP2(14.0.7173.5000)64位
首先,我会烦恼你重复你从MSDN得到的;)。 然后,我会介绍一下我发现把窗户带到前面的方法。
我也不认为有必要做你所要求的。 你想debugging你的代码,但为了debugging,我发现在执行代码的时候,它是必不可less的(大部分时间)。 并排放置窗口并使用F8来遍历代码似乎是一个更好的select。
对于我能想到的几个场景,从Excel窗口启动macros就足够了(Alt + F8)。
现在的实际答案:我不知道其他版本,但对我来说,只是激活一个窗口不会把它带到前面(Office 2016,Windows 10)。 我发现以下工作:
-
使用appactivate(可能是最好的方法,虽然我不喜欢用他们的名字指定窗口)
AppActivate Application.Caption
-
发送键
Alt
+F11
:Application.SendKeys "%{F11}"
不过,你应该检查一下,如果Excel窗口已经在前面(或者你可能会带回VBE)。 我发现的唯一方法是使用系统API:
'on top of your module Declare Function GetForegroundWindow Lib "user32" () As Long 'in your sub If GetForegroundWindow = Application.VBE.mainwindow.hwnd Then Application.SendKeys "%{F11}"
-
使用系统API
'on top of your module Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long 'in your sub SetForegroundWindow ThisWorkbook.Windows(1).hwnd
-
将可见性再次设置为虚假和真实,也将它带到我面前
ThisWorkbook.Windows(1).Visible = False ThisWorkbook.Windows(1).Visible = True
要显示VBE:
Sub ShowVBE() With Application.VBE.MainWindow .Visible = True .SetFocus End With End Sub
要激活工作簿窗口,请执行以下操作:
Sub ShowThisWorkbook() With ThisWorkbook .Activate .Windows(1).Activate .Windows(1).WindowState = xlMaximized .Worksheets("Sheet2").Activate ' <-- Change to your desired Worksheet name here End With End Sub