用用户表单打开多个excel文件

在vba中写一个代码,把所有其他10个excel文件中的一个excel文件的用户窗体调用到这10个excel文件中,而没有任何引用。

它显示当前Excel文件中的输出,但不显示目标文件,并显示错误,因为用户窗体已经显示,并且不能以模态方式显示窗体

Private Sub Workbook_OnClick() Dim mypath As String Dim file As Workbook Dim wb As Workbook Dim pat As String Application.ScreenUpdating = False ChDrive "C:" ChDir "C:\Users\Administrator\Desktop\John" 'john is a folder on the desktop mypath = Range("B1").Value 'mypath has the same value as chDir file = Dir(mypath & "\" & "*.xlsx") Set wb = Application.Workbooks.Open(file) If (wb.Click) Then Application.Visible = False userform1.Show End If End Sub 

因为dir()函数显示的默认目录是C:\ Users \ Administrator \ Documents \,但保存在桌面上的文件夹是C:\ Users \ Administrators \ Desktop \ John

主席先生,这是显示运行时错误 – 91是“对象variables或块variables未设置”,并突出显示行“文件= Dir(mypath&”\“&”* .xlsx“)”

 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 on the desktop 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