vba 在 Excel 中以编程方式隐藏活动工作簿
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11354456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hiding active workbook programmatically in Excel
提问by lodhb
I'm working on a VBA project based in a workbook. The code opens a new workbook and calls an external API which downloads and inserts a bunch of data in multiple worksheets of this new workbook. I deactivated Screen Updating (Application.Screenupdating = False
) so initially the screen stays focused on the original workbook while the API downloads data on the other workbook in the background. However, the screen switches to the new workbook once the API inserts data. How can I prevent this from happening? Thanks!
我正在研究基于工作簿的 VBA 项目。该代码打开一个新工作簿并调用外部 API,该 API 下载并在此新工作簿的多个工作表中插入一堆数据。我停用了屏幕更新 ( Application.Screenupdating = False
),因此最初屏幕保持专注于原始工作簿,而 API 在后台下载另一个工作簿上的数据。但是,一旦 API 插入数据,屏幕就会切换到新工作簿。我怎样才能防止这种情况发生?谢谢!
采纳答案by Jon Crowell
You either need to save and close the target workbook, or select the original workbook before screenupdating is turned back on.
您需要保存并关闭目标工作簿,或者在重新打开屏幕更新之前选择原始工作簿。
回答by Paul B.
Hiding the active workbook is possible with
隐藏活动工作簿是可能的
ActiveWorkbook.Windows(1).Visible = False
You may need to replace ActiveWorkbook
with an appropriate reference if the workbook in question is not the active one and/or add a loop like For i = 1 To ActiveWorkbook.Windows.Count
if the workbook has multiple windows.
ActiveWorkbook
如果有问题的工作簿不是活动的和/或添加一个循环,就像For i = 1 To ActiveWorkbook.Windows.Count
工作簿有多个窗口一样,您可能需要用适当的参考替换。
回答by mkingston
You could try using the ShowWindow API function:
您可以尝试使用 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
Or alternatively, the LockWindowUpdate API function (thanks to Chip Pearson, http://www.cpearson.com/excel/vbe.aspx):
或者,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