【发布时间】:2015-10-28 22:09:35
【问题描述】:
好的,我正在尝试从一个文件夹中的几个工作簿中提取数据并将它们全部放在一个文件中。当代码点击 Workbooks.Open 时,会弹出错误 1004,我确信文件没有损坏。
Sub Extract_Class_Data()
' Extract_ERP_Class_Data Macro
' Goes into all the files in the folder and extracts the data from each of them.
Dim MyFile As String
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet
Dim sourceSheetSum As Worksheet
Set sourceBook = ActiveWorkbook
Set sourceSheet = sourceBook.Sheets("Sheet1")
Dim erow
Dim Filepath As String
Filepath = ThisWorkbook.Path
MyFile = Dir(Filepath & "\*.xlsx")
Do While Len(MyFile) > 0
If MyFile = "ZZZ.xlsm" Then
Exit Sub
End If
**Workbooks.Open (MyFile)**
Worksheets(Data).Activate
Range("B6:B12").Select
Selection.Copy
ActiveWorkbook.Close savechanges:=False
sourceSheet.Select
ActiveSheet.Paste
Range("B1").Select
ActiveCell.Offset(0, 1).Select
MyFile = Dir
Loop
End Sub
【问题讨论】:
-
DIR不返回文件名但不返回路径? -
相关。您可以查看
FileSystemObject以访问磁盘上的文件。它比dir使用起来更直接 -
我应该这样做 MyFile = Dir(FilePath) 吗?