【问题标题】:Excel / VBA: How to copy data from multiple Excel filesExcel / VBA:如何从多个 Excel 文件中复制数据
【发布时间】:2016-05-13 21:32:16
【问题描述】:

我想遍历文件夹中的所有 Excel 文件,以便对每个文件执行某些操作(所有文件的布局相同,并且只有 Sheet1 上的数据)。

到目前为止,我有以下代码,它为我提供了特定文件夹中 Excel 文件的列表。 我自己想不通的是如何从每个文件中复制数据 - 特别是 我需要从每个文件中复制 A10:E50 范围内的数据,然后将其粘贴到当前文件的页面上强>(都在彼此之下)。

有人可以帮我解决这个问题吗?

我当前的代码:

Sub FindFiles()
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim ws As Worksheet

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set ws = Worksheets.Add

    Set objFolder = objFSO.GetFolder("C:\Users\mo\Desktop\Test-Import\")
    'ws.Cells(1, 1).Value = "The folder " & objFolder.Name & " contains the following Excel files:"

    For Each objFile In objFolder.Files
        ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = objFile.Name
    Next

    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing
End Sub

非常感谢您的任何帮助, 迈克

【问题讨论】:

    标签: vba excel loops foreach


    【解决方案1】:

    试试下面...

    Sub FindFiles()
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As File
    Dim ws As Worksheet
    Dim srWS As Worksheet
    Dim wb As Workbook
    Dim path As String
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    path = "  " 'Enter your path here
    Set objFolder = objFSO.GetFolder(path)
    'ws.Cells(1, 1).Value = "The folder " & objFolder.Name & " contains the following Excel files:"
    
    Set ws = Worksheets.Add
    
    For Each objFile In objFolder.Files
        rowCount = ws.UsedRange.Rows.Count
        If (objFile.Type = "Microsoft Excel Worksheet" Or objFile.Type = "Microsoft Excel Macro-Enabled Worksheet") Then
         Set wb = Application.Workbooks.Open(path & objFile.Name)
         Set srWS = wb.Sheets(1)
         srWS.Range("A10:E50").Copy
         ws.Activate
         If rowCount = 1 Then
            ws.Cells(1, 1).Value = objFile.Name
            ws.Cells(rowCount + 1, 1).Select
         Else
            ws.Cells(rowCount + 1, 1).Value = objFile.Name
            ws.Cells(rowCount + 2, 1).Select
         End If
         ActiveSheet.Paste
         Application.DisplayAlerts = False
         wb.Close
        End If
    
    Next
    Application.DisplayAlerts = True
    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing
    End Sub
    

    【讨论】:

    • 非常感谢 - 我会尽快测试并报告。
    • 欢迎您。如果你的问题解决了。请将问题标记为已回答。如果不让我知道你被困在哪里。
    • 我在我的机器上测试它工作正常。试试你的声明,即 Dim objFile As Object
    • 您确定分配给“路径”的路径中有 excel 文件吗?如果是这样,您的工作簿表 1 中是否有您在问题中提到的范围内的数据
    • 您尝试从中获取数据的文件应该是“Microsoft Excel 工作表”类型,因为如果您的文件是.xlsm,那么您应该将其更改为“Microsoft Excel 宏-启用工作表”
    【解决方案2】:

    尝试:

    Sub FindFiles()
    Dim objFolder As String, objFile As String, r As Integer, c As Integer    'r=row, c=column
    Dim ws As Worksheet
    
    Set ws = Worksheets.Add
    objFolder = "C:\Users\mo\Desktop\Test-Import\"
    objFile = Dir(objFolder)
    r = 10: c = 1
    
    While objFile <> vbNullString And c < 6
        ws.Cells(r, c).Value = objFile
        r = r + 1
        If r = 51 Then
            r = 10
            c = c + 1
        End If
        objFile = Dir
    Wend
    
    End Sub
    

    请注意,这将仅列出适合 A10:E50 的文件数(200 个文件)。如果您有超过 200 个文件,宏将不包含它们。如果您想查看更多内容,可以删除条件“And c

    【讨论】:

    • 谢谢。我没有看到在此处复制和粘贴数据的解决方案(范围 A10:E50)? ;)
    • 您知道该文件夹中有多少文件吗?您不能将可变数量的文件放入静态范围。
    • 谢谢 - 通常在 1 到 5 个之间,文件夹中的文件永远不会超过 10 个。
    • 我还是更新了我的答案,让我知道它是否适合你
    • 非常感谢 - 我会尽快测试并报告。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多