从一个文件调用另一个文件中的用户窗体

Call from one file a UserForm in another

Write VBA code that calls a UserForm of one Excel file to all the other Excel files present in a folder called john and the master Excel (consists of the following code and the user form) is present in a different location:

 Private Sub Workbook_OnClick()
    Dim mypath As String
    Dim file As String
    Dim wb As Workbook
    Dim pat As String
    Application.ScreenUpdating = False
    ChDrive "C:"
    ChDir "C:\Users\Administrator\Desktop\John"
    'john is a folder that consists of the excel files 
    mypath = Range("B1").Value
    'mypath has the same value as chDir
    file = Dir(mypath & "\" & "*.xlsx")
    Do While file <> ""
        Set wb = Application.Workbooks.Open(file)
        If Not IsEmpty(wb) Then
            Application.Visible = False
            userform1.Show
        End If
        wb.Close
        file = Dir()
    Loop
End Sub

代码将用户窗体拉到主 Excel 文件而不是 john 文件夹中的 Excel 文件。

我认为这就是您要查找的内容,如何从工作簿引用用户窗体 :

Workbooks("Book1.xls").VBProject.VBComponents.Item("UserForm1")

这是可行的,但我无法使用 .Show 方法:

Sub UFtest()
Dim UF_test As Object
    Set UF_test = ThisWorkbook.VBProject.VBComponents.Item("UserForm1")
    UF_test.Show
End Sub

这是您的完整代码:

Private Sub Workbook_OnClick()
    Dim mypath As String
    Dim file As String
    Dim wb As Workbook
    Dim pat As String
    Application.ScreenUpdating = False
    ChDrive "C:"
    ChDir "C:\Users\Administrator\Desktop\John"
    'john is a folder that consists of the excel files
    mypath = Range("B1").Value
    'mypath has the same value as chDir
    file = Dir(mypath & "\" & "*.xlsx")
    Do While file <> ""
        Set wb = Application.Workbooks.Open(file)
        If Not IsEmpty(wb) Then
            Application.Visible = False
            wb.VBProject.VBComponents.Item("UserForm1").Show
        End If
        wb.Close
        file = Dir()
    Loop
End Sub

包含要显示的用户窗体的工作簿还应该有一个显示窗体的过程。您将需要调用 this 过程来显示用户窗体。它可以是函数或子函数,我更喜欢函数,因为这样你就可以 return a success/failure 进行错误处理。

在 UserForm 工作簿中,您将在 Module1(或任何模块,但稍后需要引用它)中添加这样的过程:

Public Function ShowTheForm(Optional Modal As Boolean = False)
    'API to display a userform in THIS workbook, from another workbook

     On Error Resume Next
     UserForm1.Show IIF(Modal,vbModal,vbModeless)
     ShowTheForm = (Err.Number = 0)
End Function

然后,在试图打开此表单的工作簿中,您需要调用 ShowTheForm 过程,如下所示:

Do While file <> ""
    Set wb = Application.Workbooks.Open(file)
    If Not IsEmpty(wb) Then
        Application.Visible = False
        Application.Run("'" & wb.Name & "'!Module1.ShowTheForm")
    End If
    wb.Close
    file = Dir()
Loop

因为您已将 ShowTheForm 作为具有 return 值的函数给出,您可以捕获错误,例如:

If Not Application.Run("'" & wb.Name & "'!Module1.ShowTheForm") Then
    MsgBox "Unable to display..."
    Exit Sub
End If

Modified/enhanced 基于此处提供的一般逻辑:

http://answers.microsoft.com/en-us/office/forum/office_2007-excel/how-do-you-open-userform-in-another-workbook/e97b2c06-2a79-4cef-89bc-4f67b0f3c03a?db=5&auth=1

注意

我认为 IsEmpty 不适合对工作簿对象进行测试,您可能需要查看一下。我不确定你想用那条线做什么,但我几乎可以肯定它没有按照你认为的那样去做。