将多张工作表中的数据合并为一张工作表

时间:2018-06-20 15:03:59

标签: excel vba

我要复制到一张主表(合并)上的多张表(不包括合并和装入文件表)的单元格C21:F40中有数据。在将数据合并到“合并”工作表上之后,我想对字段c求和并删除所有重复项。

Data example

我到处都是,找不到任何可以完成上述任务的东西,因此对您的帮助将不胜感激

显式选项 Public Sub CombineDataFromAllSheets()

Dim wksSrc As Worksheet, wksDst As Worksheet
Dim rngSrc As Range, rngDst As Range
Dim lngLastCol As Long, lngSrcLastRow As Long, lngDstLastRow As Long

'Notes: "Src" is short for "Source", "Dst" is short for "Destination"

'Set references up-front
Set wksDst = ThisWorkbook.Worksheets("Import")
lngDstLastRow = LastOccupiedRowNum(wksDst) '<~ defined below (and in Toolbelt)!
lngLastCol = LastOccupiedColNum(wksDst) '<~ defined below (and in Toolbelt)!

'Set the initial destination range
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)

'Loop through all sheets
For Each wksSrc In ThisWorkbook.Worksheets

    'Make sure we skip the "Import" destination sheet!
    If wksSrc.Name <> "Import" Then

        'Identify the last occupied row on this sheet
        lngSrcLastRow = LastOccupiedRowNum(wksSrc)

        'Store the source data then copy it to the destination range
        With wksSrc
            Set rngSrc = .Range(.Cells(2, 4), .Cells(lngSrcLastRow, lngLastCol))
            rngSrc.Copy Destination:=rngDst
        End With

        'Redefine the destination range now that new data has been added
        lngDstLastRow = LastOccupiedRowNum(wksDst)
        Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)

    End If

Next wksSrc

结束子

''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''' 'INPUT:工作表,我们将搜索该工作表以查找最后一行 '输出:长行,最后占用的行 '特殊情况:如果工作表为空,则返回1 公共函数LastOccupiedRowNum(工作表作为工作表)     只要变暗     如果Application.WorksheetFunction.CountA(Sheet.Cells)<> 0然后         带表             lng = .Cells.Find(What:=“ *”,_                               之后:=。Range(“ B1”),_                               查找:= xlPart,_                               LookIn:= xlFormulas,_                               SearchOrder:= xlByRows,_                               SearchDirection:= xl上一个_                               MatchCase:= False)。行         结束于     其他         lng = 1     万一     LastOccupiedRowNum = lng 结束功能

''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''' 'INPUT:工作表,我们将搜索该工作表以查找最后一列 '输出:长行,最后占用的列 '特殊情况:如果工作表为空,则返回1 公共函数LastOccupiedColNum(Sheet作为工作表)     只要变暗     如果Application.WorksheetFunction.CountA(Sheet.Cells)<> 0然后         带表             lng = .Cells.Find(What:=“ *”,_                               之后:=。Range(“ B1”),_                               查找:= xlPart,_                               LookIn:= xlFormulas,_                               SearchOrder:= xlByColumns,_                               SearchDirection:= xl上一个_                               MatchCase:= False).Column         结束于     其他         lng = 1     万一     LastOccupiedColNum = lng 结束功能

1 个答案:

答案 0 :(得分:0)

本文的某些内容对于您的确切需求含糊不清,但我认为您可以在此处找到所需的内容:

Merge Data From Multiple Sheets Onto One Sheet

但是,如果您不熟悉VBA,甚至完全不了解该网站,可能也会很困难。