【问题标题】:Combine many Excel files into one将多个 Excel 文件合并为一个
【发布时间】:2015-01-12 08:01:47
【问题描述】:

我正在寻找一种将多个 Excel 文件合并为一个的方法。

我有超过 10 个具有相同数量的列和类似信息的文件。我知道我可以打开所有这些并复制并粘贴到其中,但我正在寻找一种更快的方法。

我也知道我可以将它们更改为 .csv 文件并使用 cmd 命令将它们合并为一个。

我想要一种无需打开所有文件的方法。

【问题讨论】:

  • 每个文件是否只使用一个工作表?
  • 您是想将 10 个工作表复制到一个工作簿(10 个选项卡)中,还是要将 10 个工作表合并到一个工作表中?
  • 每个文件只有一个工作表,我想将所有 10 个工作表合并到一个工作表中
  • 如果您使用的是 csv 文件而不是 Excel 文件,那么您可以将它们全部放在同一个目录中,打开一个 cmd 窗口,导航到该目录,然后输入 copy *.csv combined.csv

标签: excel merge


【解决方案1】:

您还没有说每个文件是否只有一张纸。无论如何,假设它只有一个(并且总是第一个),您也许可以使用下面的代码。请将“C:\MyDirectory\MyFiles”替换为保存文件的文件夹的适当路径。

注意:文件打开程序不是我的工作。我在这里得到了帮助: http://software-solutions-online.com/2014/03/05/list-files-and-folders-in-a-directory/

希望这会有所帮助...

'note: the file opening procedure is not my work. I got help here:
'http://software-solutions-online.com/2014/03/05/list-files-and-folders-in-a-directory/

Sub Merge_Files()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Long
Dim col_no As Long, row_no As Long
Dim arr_ws As Variant
Dim ws1 As Worksheet, wb1 As Workbook
Dim col_ws1 As Long, row_ws1 As Long

Set wb1 = ThisWorkbook
Set ws1 = ActiveSheet

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("C:\MyDirectory\MyFiles")
i = 0
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
    i = i + 1
    Workbooks.Open Filename:=objFile
    row_no = ActiveSheet.Range(Cells(Rows.Count, 1), Cells(Rows.Count, 1)).End(xlUp).Row
    col_no = ActiveSheet.Range(Cells(1, Columns.Count), Cells(1, Columns.Count)).End(xlToLeft).Column
    arr_ws = ActiveSheet.Range(Cells(1, 1), Cells(row_no, col_no))
    ActiveWorkbook.Close savechanges = no
    ws1.Activate
        If ws1.Range("A1").Value <> "" Then
            row_ws1 = ActiveSheet.Range(Cells(Rows.Count, 1), Cells(Rows.Count, 1)).End(xlUp).Row + 1
            col_ws1 = ActiveSheet.Range(Cells(1, Columns.Count), Cells(1, Columns.Count)).End(xlToLeft).Column
        Else
            row_ws1 = 1
            col_ws1 = 1
        End If
    ws1.Range(Cells(row_ws1, 1), Cells(row_ws1 + row_no - 1, col_no)) = arr_ws
    
Next objFile
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-12
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多