Hiding active workbook programmatically in Excel

后端 未结 3 1458
旧时难觅i
旧时难觅i 2020-12-03 17:19

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

相关标签:
3条回答
  • 2020-12-03 18:00

    You either need to save and close the target workbook, or select the original workbook before screenupdating is turned back on.

    0 讨论(0)
  • 2020-12-03 18:04

    You could try using the ShowWindow API function:

    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):

    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
    
    0 讨论(0)
  • 2020-12-03 18:18

    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.

    0 讨论(0)
提交回复
热议问题