在vba中编写一个代码,将一个excel文件的用户表单调用到所有其他10个excel文件,而不在这10个excel文件中有任何引用。
它显示当前excel文件中的输出但不显示在目标文件中,并显示错误,因为Userform已经显示并且无法以模态方式显示表单
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
提到了chDir,因为使用dir()函数显示的默认目录是C:\ Users \ Administrator \ Documents \但是保存在桌面上的文件夹是C:\ Users \ Administrators \ Desktop \ John
先生,它显示的是运行时错误 - 91是“对象变量或未设置块变量”并突出显示行“file = Dir(mypath&”\“&”* .xlsx“) “
答案 0 :(得分:0)
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