我正在研究基于工作簿的 VBA 项目。该代码会打开一个新工作簿并调用一个外部 API,该 API 会在这个新工作簿的多个工作表中下载并插入一堆数据。我停用Application.Screenupdating = False
了屏幕更新(但是,一旦 API 插入数据,屏幕就会切换到新工作簿。我怎样才能防止这种情况发生?谢谢!
问问题
75710 次
3 回答
27
可以使用隐藏活动工作簿
ActiveWorkbook.Windows(1).Visible = False
如果有问题的工作簿不是活动工作簿和/或添加一个循环,例如工作簿有多个窗口,您可能需要ActiveWorkbook
用适当的参考替换。For i = 1 To ActiveWorkbook.Windows.Count
于 2012-07-06T07:15:57.553 回答
5
您可以尝试使用 ShowWindow API 函数:
Public Declare Function ShowWindow Lib "user32.dll" _
(ByVal HWND As Long, ByVal nCmdShow As Long) As Long
Const SW_HIDE as Long = 0
Const SW_SHOW as Long = 5
ShowWindow otherWorkbookApplication.Hwnd, SW_HIDE
'Your code here
ShowWindow otherWorkbookApplication.Hwnd, SW_SHOW
或者,LockWindowUpdate API 函数(感谢 Chip Pearson,http ://www.cpearson.com/excel/vbe.aspx ):
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal ClassName As String, ByVal WindowName As String) As Long
Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hWndLock As Long) As Long
Sub EliminateScreenFlicker()
Dim VBEHwnd As Long
On Error GoTo ErrH:
Application.VBE.MainWindow.Visible = False
VBEHwnd = FindWindow("wndclass_desked_gsk", _
Application.VBE.MainWindow.Caption)
If VBEHwnd Then
LockWindowUpdate VBEHwnd
End If
'''''''''''''''''''''''''
' your code here
'''''''''''''''''''''''''
Application.VBE.MainWindow.Visible = False
ErrH:
LockWindowUpdate 0&
End Sub
于 2012-07-06T01:54:40.470 回答
3
您要么需要保存并关闭目标工作簿,要么在重新打开屏幕更新之前选择原始工作簿。
于 2012-07-06T01:29:43.483 回答