合并多个excel文件,同时仅将头标行保留在第一个文件中

时间:2014-02-21 10:39:43

标签: excel vba excel-vba

我尝试了很多,但找不到满意的答案。对不起,如果是重新发布。

我基本上想要的是将多个excel文件合并到一个工作簿中。我只想保留第一个excel文件中的标题行,并忽略其余excel文件的标题行(因为它们都是相同的)。所以最终结果应该是来自第一个excel文件的Header +数据,而剩下的excel文件中我只需要数据行,而不是第一行的列标题与第一个文件类似。 下面的副本粘贴所有excel文件中的所有行和列。谢谢你的帮助。

For wbCounter = 1 To UBound(books)

                Set wbSource = oExcel.Workbooks.Open(books(wbCounter))
                For wsCounter = 1 To wbSource.Sheets.Count
                    Set wsSource = wbSource.Sheets(wsCounter)

                    If wsSource.Name Like selectSheetStr Then
                        emptySheet = True

                        If cbOptionIgnoreEmpty.Value = True And wsSource.UsedRange.Address = "$A$1" Then
                            emptySheet = True
                        Else
                            emptySheet = False
                        End If

                        If emptySheet = False Then
                            mergedWorksheetName = wsSource.Name

                            sheetExist = SheetExists(mergedWorksheetName, wbResult)
                            If (cbOptionAppendData.Value = True And sheetExist = True) Then
                                Set wsMergeResult = wbResult.Sheets(mergedWorksheetName)
                                wsSource.UsedRange.Copy
                                wsMergeResult.Cells(wsMergeResult.UsedRange.Rows.Count + 1, 1).Resize(wsSource.UsedRange.Rows.Count, wsSource.UsedRange.Columns.Count).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone
                                mergedWorksheetCount = mergedWorksheetCount + 1
                                'Name of Worksheet
                                wsResult.Cells(mergedWorksheetCount + 1, 1) = wsMergeResult.Name
                                'Fullpath of Workbook
                                wsResult.Cells(mergedWorksheetCount + 1, 2) = wbSource.FullName
                            Else
                                wsSource.Copy After:=wbResult.Sheets(wbResult.Sheets.Count)
                                mergedWorksheetCount = mergedWorksheetCount + 1
                                wsResult.Cells(mergedWorksheetCount + 1, 1) = .ActiveSheet.Name
                                wsResult.Cells(mergedWorksheetCount + 1, 2) = wbSource.FullName
                            End If
                        End If
                    End If
                Next wsCounter
                wbSource.Close SaveChanges:=False
            Next wbCounter

2 个答案:

答案 0 :(得分:0)

尝试更换线路......

wsSource.UsedRange.Copy

...与:

wsSource.UsedRange.Resize(wsSource.UsedRange.Rows.Count - 1, wsSource.UsedRange.Columns.Count).Offset(1, 0).Copy

这应该复制使用的范围减去第3行。

答案 1 :(得分:-1)

此代码将为您提供特定文件夹中所有文件的路径

Option Explicit
Sub CountRows()

    Dim MyObject As Scripting.FileSystemObject
    Set MyObject = New Scripting.FileSystemObject
    Dim mySource As Folder
    Dim myFile As Scripting.File

    Dim strPath As String

    Set mySource = MyObject.GetFolder("D:\") ' or any other folder
    For Each myFile In mySource.Files
        strPath = myFile.Path
   Next
End Sub

此代码将打开这些工作簿并修改其单元格

dim wrkbook as workbook
set wrkbook = workbooks.open(strPath) ' the path comes from the code above
'modify the workbook
wrkbook.worksheets.item(1).cells(1, 1) = "something"