我是Macros的新手,目前正在尝试使用Excel宏从多个Excel报告中生成摘要表。所有文件都在sharepoint上。我已经将sharepoint文件夹映射到我的计算机并在代码中提供了路径。当我运行代码时,它对前几个文件运行正常,然后随机给出错误1004:'找不到文件'。我已经验证该文件存在于该位置,并且提供的路径也是正确的路径,因为在另一次迭代期间,代码复制该文件的数据并在其他文件上给出错误。
下面复制的是我正在使用的代码部分。我在线上遇到错误
Set WB = Workbooks.Open(Directory1 & MyFile)
请检讨。感谢任何帮助。
非常感谢您的时间和兴趣!
Option Explicit
Option Compare Text
Sub GenerateSummary()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Dim Directory1, Directory2 As Variant
Dim MyFile As Variant
Dim WB, SummarySheet As Workbook
Set SummarySheet = Workbooks.Add ' Create new workbook where summary sheet is generated
' Opening NPI Files one after another
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then
MsgBox ("You did not select a folder")
SummarySheet.Close
Exit Sub
End If
Directory2 = .SelectedItems(1) '& "\"
Directory2 = Right(Directory2, 28)
End With
Directory1 = "\\hp.sharepoint.com\teams\NPICostReporting\Shave Platforms\Shared Documents\" & Directory2 & "\"
MyFile = Dir(Directory1 & "*.xlsb")
Do While MyFile <> ""
Set WB = Workbooks.Open(Directory1 & MyFile)
' Other operations are performed below this line
MyFile = Dir()
WB.Close
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub