簡體   English   中英

從另一個文件中調用UserForm

[英]Call from one file a UserForm in another

將調用一個Excel文件的UserForm的VBA代碼寫入名為john的文件夾中存在的所有其他Excel文件,並且主Excel(包含以下代碼和用戶表單)位於不同的位置:

 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文件中提取UserForm。

包含要顯示的UserForm的工作簿還應具有顯示表單的過程。 您需要調用過程以顯示userform。 它可以是函數或子函數,我更喜歡函數,因為這樣您就可以返回錯誤處理的成功/失敗。

在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作為具有返回值的函數,所以可以捕獲錯誤,例如:

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

基於此處提供的一般邏輯修改/增強:

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不是對工作簿對象的適當測試,你可能想要查看它。 我不確定你要用這條線做什么,但我幾乎可以肯定它沒有做你認為它正在做的事情。

我認為這正是您要尋找的,如何從工作簿中引用UserForm

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM