VBA运行时错误1004“对不起,我们可以find文件..”。 VBA通过一个文件夹循环编译所有的数据到一个Excel表单

我创build一个VBA代码,循环通过相同的文件(差异数据)的文件夹,并将其编译到一个Excel表。 但是,有一个错误:

“运行时错误1004:我们无法find[文件]”

Sub LoopThroughFolderAllData() Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook Dim Rws As Long, Rng As Range Dim myPath As String Dim FldrPicker As FileDialog Set Wb = ThisWorkbook Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker) With FldrPicker .Title = "Select Folder with IQC Data" .AllowMultiSelect = False If .Show <> -1 Then GoTo NextCode myPath = .SelectedItems(1) & "\" End With 'In Case of Cancel NextCode: myPath = myPath If myPath = "" Then GoTo ResetSettings 'Target Path with Ending Extention MyFile = Dir(myPath & "*.xls*") Application.ScreenUpdating = 0 Application.DisplayAlerts = 0 Do While MyFile <> "" Set Wb = Workbooks.Open(Filename:=myPath & MyFile) Workbooks.Open (MyFile) With Worksheets("All Data") Rws = .Cells(Rows.Count, "A").End(xlUp).Row Set Rng = Range(.Cells(2, 1), .Cells(Rws, 70)) Rng.Copy Wb.Worksheets("All Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) ActiveWorkbook.Close True End With MyFile = Dir() Loop 'Message Box when tasks are completed MsgBox "Task Complete!" ResetSettings: 'Reset Macro Optimization Settings Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub 

错误发生在行上:

 Workbooks.Open (MyFile) 

任何帮助表示赞赏!

修改Do While代码如下:

 Do While MyFile <> "" Set Wb = Workbooks.Open(Filename:=myPath & MyFile) With Worksheets("All Data") Rws = .Cells(Rows.Count, "A").End(xlUp).Row Set Rng = Range(.Cells(2, 1), .Cells(Rws, 70)) Rng.Copy Wb.Worksheets("All Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) wb.Close True End With MyFile = Dir() Loop 

我不认为你需要这条线

  myPath = myPath