2

我需要打开文件夹中的所有 Excel 文件,并将每个文件的第一行、第 57 行、第 72 行、第 73 行和第 74 行复制到同一个工作簿中。

这是我到目前为止所拥有的:

Dim i As Integer
Dim wbOpen As Workbook
Dim MyPath As String
Dim strFilename As String
MyPath = "C:\foldername" 'the folder
strFilename = Dir(MyPath & "\*.xls") 'all excel files in folder

Do Until strFilename = ""

    'here it gives an error, saying it can't open file,
    ' even though it apparently has found it
    wbOpen = Workbooks.Open(strFilename)

    'copy relevant text
    Workbooks(2).Activate
    Sheets("blad1").Rows(1, 57, 72, 73, 74).Copy
    Workbooks(1).Activate
    'select the first empty row
    irow = ActiveSheet.UsedRange.Rows.Count + 1
    'insert the copied
    irow.Insert Shift:=xlDown
    'close the workbook without saving
    Workbooks(2).Close SaveChanges:=False
    Close

Loop
4

1 回答 1

1

试试这种方式:

MyPath = "C:\foldername" 'the folder
strFilename = Dir(MyPath & "\*.xls") 'all excel files in folder

Do Until strFilename = ""
...
strFilename=Dir()
Loop

告诉我这是否有效

Sheets("blad1").Rows(1, 57, 72, 73, 74).Copy编辑 :修复Sheets("blad1").Rows("1, 57, 72, 73, 74").Copy

于 2012-10-12T16:15:49.263 回答