简体   繁体   English

将多个 Excel 工作簿合并为一个工作簿

[英]Combine multiple Excel workbooks into a single workbook

I am a novice at Visual Basic.我是 Visual Basic 的新手。 I can use either Excel 2010 or Excel 2013 for this task.我可以使用 Excel 2010 或 Excel 2013 来完成这项任务。

I have dozens of workbooks with data on the first worksheet of each.我有几十个工作簿,每个工作簿的第一个工作表上都有数据。 For example One.xlsx, Two.xlsx, Three.xlsx, Four.xlsx each contain information on their respective Sheet1.例如 One.xlsx、Two.xlsx、Three.xlsx、Four.xlsx 每个都包含其各自 Sheet1 上的信息。

I need the information on Sheet1 from each workbook to be combined into a single workbook with sheets that are named from the file name of the original workbook.我需要将每个工作簿中 Sheet1 上的信息合并到一个工作簿中,这些工作簿的工作表以原始工作簿的文件名命名。 So for example combined.xlsx would have 4 sheets named One, Two, Three, Four.例如,combined.xlsx 将有 4 张名为一、二、三、四的工作表。 In every case all information on the underlying worksheets should be copied and combined in the new Workbook as shown below.在每种情况下,底层工作表上的所有信息都应复制并合并到新工作簿中,如下所示。

  • The Format I need我需要的格式

在此处输入图片说明

I found this Macro / Add-In online that gets me close to what I need using the open files add in choice.我在网上找到了这个宏/插件,它使我接近使用打开文件插件选项所需的内容。

http://www.excelbee.com/merge-excel-sheets-2010-2007-2013#close http://www.excelbee.com/merge-excel-sheets-2010-2007-2013#close

The Open Files Add-In successfully allows me to aggregate the various Workbook's worksheets into a single workbook. Open Files Add-In 成功地允许我将各种工作簿的工作表聚合到一个工作簿中。 However the tabs are not named from the name of the original file.但是,选项卡不是根据原始文件的名称命名的。

  • Correct aggregation of sheets, but incorrect worksheet names.正确聚合工作表,但不正确的工作表名称。

在此处输入图片说明

For now all the underlying Workbooks will be in the same folder.现在所有底层工作簿都将位于同一个文件夹中。 The ability to browse and select the files would be nice if this ever changes but if that is too difficult, just indicating the directory path in the Visual Basic code would work.如果这发生变化,浏览和选择文件的能力会很好,但如果这太困难,只需在 Visual Basic 代码中指示目录路径即可。 As far as the resultant combined output probably ought to be a new workbook, the filename of the new workbook isn't that important.至于最终的组合输出可能应该是一个新工作簿,新工作簿的文件名并不那么重要。 It could be called combined.xlsx for example.例如,它可以被称为combined.xlsx。

The following accomplishes the task.下面完成任务。

Option Explicit

Private Sub CommandButton1_Click()

Dim directory As String, fileName As String, sheet As Worksheet, total As Integer
Dim WrdArray() As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

directory = "c:\test\"
fileName = Dir(directory & "*.xl??")

Do While fileName <> ""
    Workbooks.Open (directory & fileName)
        WrdArray() = Split(fileName, ".")
        For Each sheet In Workbooks(fileName).Worksheets
        Workbooks(fileName).ActiveSheet.Name = WrdArray(0)
            total = Workbooks("import-sheets.xlsm").Worksheets.Count
            Workbooks(fileName).Worksheets(sheet.Name).Copy after:=Workbooks("import-sheets.xlsm").Worksheets(total)

            GoTo exitFor:

        Next sheet

exitFor:
    Workbooks(fileName).Close
    fileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

In Excel press Alt+F11 , this will open the Excel VBA editor.在 Excel 中按Alt+F11 ,这将打开 Excel VBA 编辑器。

Article http://www.excel-spreadsheet.com/vba/debugging.htm explains some basics how to use it.文章http://www.excel-spreadsheet.com/vba/debugging.htm解释了如何使用它的一些基础知识。

In Module1 there are 2 short subroutines opensheets and merge containing ~50 lines of code.Module1有 2 个简短的子程序opensheetsmerge包含约 50 行代码。

Use F1 with cursor within words you don't understand, to learn what it means.在您不理解的单词内使用带有光标的F1 ,以了解其含义。

Once you understand what the code does, you can tailor it to your needs.一旦你理解了代码的作用,你就可以根据自己的需要定制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM