【VBA】获取指定目录下的Excel文件,并合并所有excel中的内容。

1.新建一个excel表格。并创建两个Sheet,名字分别命名为FileList 和 All information。

2.按ALT+F11进入  VBA编程模块,插入模块。 

3.将如下 第五部分代码复制到模块中。  点击运行即可,然后就能提取指定目录下的所有excel文件信息并合并到一起输出到“All information” 中。

4.运行过程中,在弹窗中输入 想要提取信息的路径地址。

5.说明

这个脚本的逻辑分为两部分:

  • 首先是提取文件夹中所有文件的基本信息,并将其填充到"FileList"工作表中。
  • 之后,它将这些文件打开并将它们的内容合并到"All information"工作表中。
Sub CombinedScript()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    On Error Resume Next
    
    ' Step 1: Extracting files from folders
    Dim arr(1 To 10000) As String
    Dim arr1(1 To 100000, 1 To 6) As String
    Dim fso As Object, myfile As Object
    Dim f, i, k, f2, f3, x
    Dim q As Integer
    
    arr(1) = Application.InputBox("Please enter the path to scan") & "\"
    i = 1
    k = 1
    Do While i < UBound(arr)
        If arr(i) = "" Then Exit Do
        f = Dir(arr(i), vbDirectory)
        Do
            If InStr(f, ".") = 0 And f <> "" Then
                k = k + 1
                arr(k) = arr(i) & f & "\"
            End If
            f = Dir
        Loop Until f = ""
        i = i + 1
    Loop
    
    ' Extract files information
    Set fso = CreateObject("Scripting.FileSystemObject")
    For x = 1 To UBound(arr)
        If arr(x) = "" Then Exit For
        f3 = Dir(arr(x) & "*.*")
        Do While f3 <> ""
            If InStr(f3, ".") > 0 Then
                q = q + 1
                arr1(q, 5) = arr(x) & f3
                Set myfile = fso.GetFile(arr1(q, 5))
                arr1(q, 1) = f3
                arr1(q, 2) = myfile.Size
                arr1(q, 3) = myfile.DateCreated
                arr1(q, 4) = myfile.DateLastModified
                arr1(q, 6) = myfile.DateLastAccessed
            End If
            f3 = Dir
        Loop
    Next x
    
    Sheets("FileList").Range("A2").Resize(1000, 6).ClearContents
    Sheets("FileList").Range("A2").Resize(q, 6) = arr1
    
    ' Step 2: Combine information into "All information" sheet
    If Sheets("All information").FilterMode = True Then
        Sheets("All information").ShowAllData
    End If
    Sheets("All information").Range("A2:ZZ100000").ClearContents
    
    Dim currentFile As Object
    Dim targetRow As Integer
    Dim temRowCount As Integer
    targetRow = 2
    
    For fileCount = 2 To Sheets("FileList").Cells(10000, 1).End(xlUp).Row
        Set currentFile = Application.Workbooks.Open(Sheets("FileList").Cells(fileCount, 5))
        For sheetscount = 1 To currentFile.Sheets.Count
            temRowCount = currentFile.Sheets(sheetscount).UsedRange.Rows.Count
            
            ' Copy content
            currentFile.Sheets(sheetscount).UsedRange.Copy
            ThisWorkbook.Sheets("All information").Cells(targetRow, 3).PasteSpecial (xlPasteValues)
            
            ' Set sheet and workbook information
            ThisWorkbook.Sheets("All information").Range("A" & targetRow & ":A" & targetRow + temRowCount).Value = currentFile.Name
            ThisWorkbook.Sheets("All information").Range("B" & targetRow & ":B" & targetRow + temRowCount).Value = currentFile.Sheets(sheetscount).Name
            
            targetRow = targetRow + temRowCount
        Next sheetscount
        
        currentFile.Close False
    Next fileCount
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Excel VBA合并文件夹下的文件,首先需要使用VBA代码来遍历子文件夹和文件。可以使用FileSystemObject对象来处理文件文件夹。 首先,用FileSystemObject对象获取目标文件的所有文件和子文件夹。然后,再进一步遍历每个子文件夹,获取文件。 接下来,可以使用Workbooks.Open方法逐个打开文件,然后将其内容复制到合并文件。在复制内容之前,需要注意对文件的格式进行处理,以确保合并后的文件格式不会混乱。 在复制完所有文件内容后,可以使用SaveAs方法将合并后的文件保存到指定的位置。 最后,关闭所有打开的文件,并释放FileSystemObject对象的引用,以确保代码执行完毕后不会出现内存泄漏问题。 以下是一个简单的VBA示例代码,用于合并文件夹下的所有文件: ```vba Sub MergeFilesInSubfolders() Dim fso As Object Dim sourceFolder As Object Dim subFolder As Object Dim file As Object Dim mergeWorkbook As Workbook '设置目标文件夹路径 Set fso = CreateObject("Scripting.FileSystemObject") Set sourceFolder = fso.GetFolder("C:\YourSourceFolder") Set mergeWorkbook = Workbooks.Add '创建一个新的工作簿作为合并后的文件 '遍历子文件夹和文件 For Each subFolder In sourceFolder.SubFolders For Each file In subFolder.Files '逐个打开文件,并将内容复制到合并文件 Workbooks.Open (file.Path) ActiveWorkbook.Sheets(1).Copy After:=mergeWorkbook.Sheets(mergeWorkbook.Sheets.Count) ActiveWorkbook.Close False Next file Next subFolder '保存合并后的文件 mergeWorkbook.SaveAs "C:\MergedFile.xlsx" '清理对象引用 Set mergeWorkbook = Nothing Set file = Nothing Set subFolder = Nothing Set sourceFolder = Nothing Set fso = Nothing End Sub ``` 以上代码是一个简单的示例,具体的实现方式会根据需求和实际情况而有所不同。需要根据具体情况调整文件路径、保存路径和文件格式等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值