将多个Excel文件中相同结构的工作表附加到一个

时间:2018-04-12 12:21:39

标签: excel vba excel-vba merge excel-2010

我想将几个excel文件合并为一个。我想要组合的excel文件具有相同的工作表。所以在这一点上,我想组合来自不同excel文件的相同表格。

我有这个代码要合并,但是这个代码只是结合了excel文件中最新的活动工作表:

Sub simpleXlsMerger()
    Dim bookList As Workbook
    Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
    Application.ScreenUpdating = False
    Set mergeObj = CreateObject("Scripting.FileSystemObject")
    Set dirObj = mergeObj.Getfolder("C:\Users\5545\Desktop\MI")
    Set filesObj = dirObj.Files
    For Each everyObj In filesObj
        Set bookList = Workbooks.Open(everyObj)
        Range("A2:IV" & Range("A28").End(xlUp).Row).Copy
        ThisWorkbook.Worksheets(1).Activate
        Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
        Application.CutCopyMode = False
        bookList.Close
    Next
End Sub

如何在该代码中指定我希望的sheet("Day 1")?我想要组合的工作表名称是“第1天”

2 个答案:

答案 0 :(得分:1)

写:

With bookList.Worksheets("Day 1")
    .Range("A2:IV" & .Range("A28").End(xlUp).Row).Copy
End With

而不是:

Range("A2:IV" & Range("A28").End(xlUp).Row).Copy

Range之前的两个点很重要,它们确保您引用两次正确的工作表:

enter image description here

如果没有它们,代码就会引用ActiveSheet

答案 1 :(得分:0)

您的代码看起来像是将代码部分复制在一起,如果我错了,请纠正我。因此,我应用了您需要的更改并对代码中的注释和重命名部分进行了更改以便更好地理解。如果您没有找到这个有用的,请发表评论,我将删除答案:)

Sub simpleXlsMerger()

    Application.ScreenUpdating = False
    'Define a Workbook Object for the Workbooks to pull data from
    Dim sourceWorkbook As Workbook
    'Define a Workbook Object of the Workbook to summarize data in
    Dim summaryWorkbook As Workbook
    'Set the summary Workbook to ThisWorkbook
    Set summaryWorkbook = ThisWorkbook

    Dim folderObject As Object
    Dim myFolder As Object
    Dim myFiles As Object
    Dim everyObj As Object
    Dim folderPath As String

    folderPath = "C:\Users\5545\Desktop\MI"

    Set folderObject = CreateObject("Scripting.FileSystemObject")
    Set myFolder = folderObject.Getfolder("C:\Users\5545\Desktop\MI")
    Set myFiles = myFolder.Files

    'For each file in your directory you set the sourceWorkbook equal to that file
    For Each File In myFiles
        Set sourceWorkbook = Workbooks.Open(File)
        'You define the range within this Workbook and copy the range
        sourceWorkbook.Range("A2:IV" & Range("A28").End(xlUp).Row).Copy
        'You append the summary sheet with the copied data
        summaryWorkbook.Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
        'You close the file and open the next in the next loop
        sourceWorkbook.Close
    Next
    summaryWorkbook.Activate

End Sub

初学者访问工作簿,表格和范围的一般注释:

'Define an object container for workbooks,sheets and ranges you work with
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range

'A worksheet can be assigned by Name when its open, set to the Workbook the code is within, open a Workbook by Path or add a new one
'The workbook can then be accessed by using the variable e.g. wb
Set wb = ThisWorkbook
Set wb = Workbooks("WorkbookName")
Set wb = Workbooks.Open("FilePathIncludingNameAndFileType")
Set wb = Workbooks.Add

'Similarly worksheet objects can be assigned by Name, Index or added. Dont forget to use wb. to reference in which workbook to look for the sheet
Set ws = wb.Sheets("NameOfSheet")
Set ws = wb.Sheets(IndexOfSheet)
Set ws = wb.Sheets.Add

'Ranges are Objects within Worksheets and can be accessed by A1 notation e.g. "A1:C10" or by cell reference e.g. (Cells(1,1), Cells(10,3))
Set rng = ws.Range("A1NotationOfRange")
Set rng = ws.Range(Cells(RowIndex, ColumnIndex), Cells(RowIndex, ColumnIndex))