工作簿不显示何时从表单打开

时间:2017-11-05 19:45:48

标签: excel vba forms excel-vba

我有两本工作簿。 Workbook1运行并显示Userform1,同时使用此Sub过程隐藏工作簿1:

Private Sub CommandButton2_Click()
    Application.Visible = True
    MainForm.Hide
    On Error Resume Next
End Sub

但是我还需要使用带有以下代码的命令按钮从userform1打开Workbook2:

    Private Sub CommandButton4_Click()

        Dim Finfo As String
        Dim FilterIndex As Integer
        Dim Title As String
        Dim Filename As Variant
        Dim wb As Workbook


'      Setup the list of file filters

        Finfo = "Excel Files (*.xlsx),*xlsx,"

'      Display *.* by default

        FilterIndex = 1*

'       Set the dialog box caption

        Title = "Select a File to Open"

    '   Get the Filename
        Filename = Application.GetOpenFilename(Finfo, _
        FilterIndex, Title)

'       Handle return info from dialog box

        If Filename = False Then
        MsgBox "No file was selected."
        Else
        MsgBox "You selected " & Filename
        End If

        Set wb = Workbooks.Open(Filename)

        End Sub

我的问题是,因为commandbutton2的部分或代码具有代码的一部分Application.Visible = False当我打开Workbook2时我无法看到它,除非我关闭userform1。因此,我希望保持Workbook1 Hide,同时我能够查看和使用Workbook2。谢谢

1 个答案:

答案 0 :(得分:0)

您在这里寻找的是拥有无模式用户形式,这样您就可以在显示对话框时继续在当前应用程序的其他位置工作" (More infos

为此,当您使用show方法显示表单时,您需要添加vbModeless选项:

MainForm.Show(vbModeless)

现在,如何始终保持用户形态(在所有窗口之上)? 假设您使用的是Windows,那么您将使用一些API声明。如果您不确切知道这是什么,请不要担心,您可以按照以下说明操作:

1)将以下代码复制到单独的模块中

Option Explicit

Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1

Public Enum HWND_TYPE
    HWND_TOP = 0
    HWND_NOTOPMOST = -2
    HWND_TOPMOST = -1
    HWND_BOTTOM = 1
End Enum

#If VBA7 Then

    Public Declare PtrSafe Function SetWindowPos Lib "user32" _
        (ByVal hWnd As LongPtr, _
        ByVal hWndInsertAfter As LongPtr, _
        ByVal x As Long, _
        ByVal Y As Long, _
        ByVal cx As Long, _
        ByVal cy As Long, _
        ByVal uFlags As Long) As Long

    Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, _
        ByVal lpWindowName As String) As LongPtr

#Else

    Public Declare Function SetWindowPos Lib "user32" _
        (ByVal hWnd As Long, _
        ByVal hWndInsertAfter As Long, _
        ByVal X As Long, _
        ByVal Y As Long, _
        ByVal cx As Long, _
        ByVal cy As Long, _
        ByVal uFlags As Long) As Long

    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long

#End If

2)将此代码复制到userform代码模块中的AlwaysOnTop过程:

Private Sub AlwaysOnTop(caption As String)
'PURPOSE: This function allows the userform to remain on top of all windows - Adjusted
'REFERENCE: https://www.mrexcel.com/forum/excel-questions/386643-userform-always-top-2.html

    #If VBA7 Then
        Dim hWnd As LongPtr
    #Else
        Dim hWnd As Long
    #End If
    Dim lResult As Boolean

    If Val(Application.Version) >= 9 Then
        hWnd = FindWindow("ThunderDFrame", caption)
    Else
        hWnd = FindWindow("ThunderXFrame", caption)
    End If

    If hWnd <> 0 Then

        lResult = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)

    Else

        MsgBox "AlwaysOnTop: userform with caption '" & caption & "' not found"

    End If

End Sub

3)在UserForm_Initialize中的某处使用AlwaysOnTop过程。即:

Private Sub UserForm_Initialize()

    'Your code

    AlwaysOnTop Me.caption

End Sub