问题
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
The code is pulling the UserForm on the master Excel file instead of the Excel files present in john
folder.
回答1:
The workbook which contains the UserForm that you want to display should also have a procedure which displays the form. You will need to call this procedure to display the userform. It can be a function or a sub, I prefer function because then you can return a success/failure for error-handling.
In the UserForm workbook, you'll add a procedure like this in Module1 (or any module, but you'll need to reference this later):
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
Then, in the workbook which is trying to call this form open, you will need to invoke the ShowTheForm
procedure, like so:
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
Because you've given ShowTheForm
as a function with a return value, you can trap errors, for example:
If Not Application.Run("'" & wb.Name & "'!Module1.ShowTheForm") Then
MsgBox "Unable to display..."
Exit Sub
End If
Modified/enhanced based on the general logic provided here:
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
NOTE
I think IsEmpty
is not the appropriate test on a workbook object, you may want to look in to that. I'm not sure what you're trying to do with that line, but I'm almost certain it's not doing what you think it's doing.
回答2:
I think this is what you are looking for, how to reference an UserForm from a workbook :
Workbooks("Book1.xls").VBProject.VBComponents.Item("UserForm1")
this is working, but I can't manage to use .Show
method :
Sub UFtest()
Dim UF_test As Object
Set UF_test = ThisWorkbook.VBProject.VBComponents.Item("UserForm1")
UF_test.Show
End Sub
Here is your full code :
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
来源:https://stackoverflow.com/questions/33823450/call-from-one-file-a-userform-in-another