将多个.xls文件合并到一个工作表中

时间:2015-12-07 07:26:26

标签: excel vba excel-vba merge

我有一个完整的.xls文件夹,所有文件都有相同的结构(列名),我希望代码打开文件夹中的每个文件并复制sheet1的内容并将另一个excel文件粘贴到sheet1中,打开第二个文件副本并附加到表单1中。

目前我所拥有的代码是不同的表单

_orientationDidChange: function(orientation) {
    if(orientation == 'LANDSCAPE'){
       navigator.push({ name: 'test' });
    }
}

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

Sub GetSheets()
Dim WriteRow As Long, _
    LastCell As Range, _
    WbDest As Workbook, _
    WbSrc As Workbook, _
    WsDest As Worksheet, _
    WsSrc As Worksheet

Set WbDest = ThisWorkbook
Set WsDest = WbDest.Sheets.Add
WsDest.Cells(1, 1) = "Set your headers here"

Path = "C:\Users\dt\Desktop\dt kte\"
Filename = Dir(Path & "*.xls")

Do While Filename <> ""
    Set WbSrc = Workbooks.Open(Filename:=Path & Filename, ReadOnly:=True)
    Set WsSrc = WbSrc.Sheets(1)
    With WsSrc
        Set LastCell = .Cells.Find(What:="*", _
                      After:=.Range("A1"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False)
        .Range(.Range("A1"), LastCell).Copy
    End With
    With WsDest
        WriteRow = .Cells.Find(What:="*", _
                      After:=.Range("A1"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False).Row + 1
        '.Range("A" & WriteRow).Paste
        'OR
        .Range("A" & WriteRow).PasteSpecial
    End With
    '''To clear clipboard to avoid 'large clipboard' warnings on close
    Application.CutCopyMode = False

    WbSrc.Close
    Filename = Dir()
Loop

End Sub