将新工作表添加到指定文件夹中的每个Excel文件

时间:2014-01-10 01:05:36

标签: excel-vba vba excel

我需要打开所选文件夹中的所有文件,并为该文件夹中的每个文本文件创建一个新工作表。目前我有这个代码,但是当我运行它时它给了我445错误。我对vba有点新意,我该如何解决这个错误?

Sub OpenMultipleFiles()

Dim WrkBook As Workbook
Set fs = Application.FileSearch
With fs
    .SearchSubFolders = False
    .LookIn = "C:\My Documents" 'Path of folder to search
    .FileName = "*.xls" 'Limit to excel files
    If .Execute > 0 Then
        Debug.Print "There were " & .FoundFiles.Count & " file(s) found."
        For i = 1 To .FoundFiles.Count
            WrkBook = Workbooks.Open(FileName:=.FoundFiles(i))
            WrkBook.Worksheets(1).Select
            ThisWorkbook.Worksheets(1).Cells(DestinationRange) = WrkBook.Worksheets(1).Cells(SourceRange).Value
        Next i
    Else
        Debug.Print "There were no files found."
    End If
End With
End Sub

0 个答案:

没有答案