Excel 将多个工作表合并到一个工作表中

Excel 将多个工作表合并到一个工作表中,excel,vba,Excel,Vba,我希望将多个工作表合并到同一excel中的一个工作表中,但我不希望在所有工作表中的特定单词“Total”后面有一些数据。如何删除“总计”一词后的数据,然后合并所有工作表。 下面的代码用于添加多个工作表 Sub Consolidate() Dim sh As Worksheet Dim DestSh As Worksheet Dim erow As Long, lrowsh As Long, startrow As Long Dim CopyRng As Range startrow = 3 Wi

我希望将多个工作表合并到同一excel中的一个工作表中,但我不希望在所有工作表中的特定单词“Total”后面有一些数据。如何删除“总计”一词后的数据,然后合并所有工作表。 下面的代码用于添加多个工作表

Sub Consolidate()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim erow As Long, lrowsh As Long, startrow As Long
Dim CopyRng As Range
startrow = 3
With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'Deleting "Consolidate" sheet
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Consolidate").Delete
On Error GoTo 0
Application.DisplayAlerts = True



'Adding worksheet with the name "Consolidate"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Consolidate"
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
'Find the next blank or empty row on the DestSh
erow = DestSh.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
'Find the last row with data in the Sheet
lrowsh = sh.Range("A" & Rows.Count).End(xlUp).Row



Set CopyRng = sh.Range(sh.Rows(startrow), sh.Rows(lrowsh))

'copies Values / formats
CopyRng.Copy
With DestSh.Cells(erow, 1)
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With

End If
Next
End Sub
子合并()
将sh设置为工作表
将DestSh设置为工作表
暗的越长,白的越长,白的越长
暗拷贝As范围
startrow=3
应用
.ScreenUpdate=False
.EnableEvents=False
以
'删除“合并”表
Application.DisplayAlerts=False
出错时继续下一步
Active工作簿。工作表(“合并”)。删除
错误转到0
Application.DisplayAlerts=True
'正在添加名为“合并”的工作表'
设置DestSh=ActiveWorkbook.Worksheets.Add
DestSh.Name=“合并”
'循环浏览所有工作表并将数据复制到DestSh
对于ActiveWorkbook.工作表中的每个sh
如果sh.Name DestSh.Name那么
'查找DestSh上的下一个空白行或空行
erow=DestSh.Range(“A”和Rows.Count).End(xlUp).Offset(1,0).Row
'查找工作表中包含数据的最后一行
lrowsh=sh.Range(“A”和Rows.Count).End(xlUp).Row
设置CopyRng=sh.Range(sh.Rows(startrow),sh.Rows(lrowsh))
'复制值/格式
复制,复制
带DestSh.单元格(erow,1)
.Paste特殊XLPaste值
.Paste特殊XLPaste格式
Application.CutCopyMode=False
以
如果结束
下一个
端接头
有趣的工作簿整合 更改常量(
Const
)以满足您的需要

代码
子合并()
"目标"
Const cTarget As String=“Consolidate”'目标工作表名称
"来源:
常量cFR长度=3'第一行编号
Const cLRC As Variant=1'最后一行列字母/列号
Const cCrit作为String=“Total”标准
将wb设置为工作簿的目标工作簿
将wsT作为工作表的目标工作表
将ws作为工作表的当前源工作表进行调整
将eRow变长为“目标第一个空行”
变暗lRow为“长”源上次使用的行
Dim lCol作为“长”源上次使用的列
变暗RNG单元格作为“范围”单元格范围
变暗rng作为“范围”范围
“加快速度。
应用
.ScreenUpdate=False
.Calculation=xlCalculationManual
以
'创建对目标工作簿的引用。如果代码不在
'要处理的工作簿,然后使用其名称(首选)或
'活动工作簿,而不是此工作簿。
设置wb=ThisWorkbook
'注意:您可以使用代码来清除,而不是使用以下代码块
'或清除目标工作表的内容。
与wb
'删除目标工作表。
Application.DisplayAlerts=False
出错时继续下一步
.工作表(“合并”)。删除
错误转到0
Application.DisplayAlerts=True
'添加目标工作表。
设置wsT=.Worksheets.Add(在:=.Sheets(1)之前)的第一个选项卡
wsT.Name=“合并”
以
“处理错误。
关于错误转到错误处理程序
'循环浏览所有工作表。
对于wb.工作表中的每个ws
如果ws.Name wsT.Name那么
使用ws.Cells(cFR,cLRC).Resize(ws.Rows.Count-cFR+1_
ws.Columns.Count-cLRC+1)
'注意:只选择以下两行中的一行。
'查找当前工作表中第一个出现的条件。
设置rngCell=.Find(cCrit、.Cells(.Rows.Count、.Columns_
.Count),xlValues,xlother,xlByRows,xlNext)
“”查找当前工作表中最后出现的条件。
'Set rng=.Find(cCrit,xlValues,xlWhole,xlByRows_
'四十)
'清除找到条件的行下方的范围。
ws.Rows(rngCell.Row+1&“:”&ws.Rows.Count)。清除
'创建对行范围(副本范围)的引用。
设置rng=.Cells(1)。调整大小(rngCell.Row-cFR+1_
.Columns.Count-cLRC+1)
以
'创建对行最后一列中最后一个单元格的引用
'范围(复制范围的)。
设置rngCell=rng.Find(“*”,xlFormulas_
xlByColumns,xlPrevious)
'创建对复制范围的引用。
设置rng=rng.Cells(1)。调整大小(rng.Rows.Count_
rngCell.列-cLRC+1)
'在目标工作表中查找下一个空白行或空行。
eRow=wsT.Cells(wsT.Rows.Count,cLRC).End(xlUp)_
.偏移量(1,0).行
'复制范围。
收到
'在目标工作表的(第一个空行)中
带wsT.单元(eRow,1)
'首先粘贴格式,以避免在粘贴时遇到麻烦
“日期或时间。Excel可能会先将其格式化为不同的格式
'粘贴时,格式可能无法还原为所需格式。
.Paste特殊XLPaste格式
.Paste特殊XLPaste值
Application.CutCopyMode=False
以
如果结束
下一个
'转到目标工作表的顶部。
ActiveSheet.Range(“A1”)。选择
'通知用户成功(因为代码很快,您可能不知道它是否成功
“根本没有跑过。
MsgBox“操作已成功完成”,vbInformation“成功”
程序退出:
“减速。
应用
.Calculation=xlcalculation自动
.ScreenUpdate=True
以
出口接头
错误处理程序:
MsgBox“发生意外错误。错误'&错误号&':”_
&错误说明,vbCritical,“错误”
转到程序退出
端接头
有趣的工作簿整合 更改常量(
Const
)以满足您的需要

代码
Sub Consolidate()

    ' Target
    Const cTarget As String = "Consolidate"   ' Target Worksheet Name
    ' Source
    Const cFR As Long = 3             ' First Row Number
    Const cLRC As Variant = 1         ' Last-Row Column Letter/Column Number
    Const cCrit As String = "Total"   ' Criteria

    Dim wb As Workbook    ' Target Workbook
    Dim wsT As Worksheet  ' Target Worksheet
    Dim ws As Worksheet   ' Current Source Worksheet
    Dim eRow As Long      ' Target First Empty Row
    Dim lRow As Long      ' Source Last Used Row
    Dim lCol As Long      ' Source Last Used Column
    Dim rngCell As Range  ' Cell Ranges
    Dim rng As Range      ' Ranges

    ' Speed up.
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With

    ' Create a reference to Target Workbook. If the code will NOT be in the
    ' workbook to be processed, then use its name (preferable) or
    ' ActiveWorkbook instead of ThisWorkbook.
    Set wb = ThisWorkbook

    ' Note: Instead of the following with block you could use code to clear
    '       or clear the contents of the Target Worksheet.
    With wb
        'Delete Target Worksheet.
        Application.DisplayAlerts = False
        On Error Resume Next
        .Worksheets("Consolidate").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
        'Add Target Worksheet.
        Set wsT = .Worksheets.Add(Before:=.Sheets(1)) ' First Tab
        wsT.Name = "Consolidate"
    End With

    ' Handle errors.
    On Error GoTo ErrorHandler

    ' Loop through all worksheets.
    For Each ws In wb.Worksheets
        If ws.Name <> wsT.Name Then
            With ws.Cells(cFR, cLRC).Resize(ws.Rows.Count - cFR + 1, _
                    ws.Columns.Count - cLRC + 1)
                ' Note: Choose only one of the following two lines.
                'Find the first occurrence of Criteria in Current Worksheet.
                Set rngCell = .Find(cCrit, .Cells(.Rows.Count, .Columns _
                        .Count), xlValues, xlWhole, xlByRows, xlNext)
'                   'Find the last occurrence of Criteria in Current Worksheet.
'                    Set rng = .Find(cCrit, , xlValues, xlWhole, xlByRows, _
'                            xlPrevious)
                ' Clear the range below the row where Criteria was found.
                ws.Rows(rngCell.Row + 1 & ":" & ws.Rows.Count).Clear
                ' Create a reference to Row Range (of Copy Range).
                Set rng = .Cells(1).Resize(rngCell.Row - cFR + 1, _
                        .Columns.Count - cLRC + 1)
            End With
            ' Create a reference to last cell in last column of Row
            ' Range (of Copy Range).
            Set rngCell = rng.Find("*", , xlFormulas, , _
                    xlByColumns, xlPrevious)
            ' Create a reference to Copy Range.
            Set rng = rng.Cells(1).Resize(rng.Rows.Count, _
                    rngCell.Column - cLRC + 1)

            'Find the next blank or empty row in Target Worksheet.
            eRow = wsT.Cells(wsT.Rows.Count, cLRC).End(xlUp) _
                    .Offset(1, 0).Row
            ' Copy Copy Range.
            rng.Copy
            ' In (First Empty Row of) Target Worksheet
            With wsT.Cells(eRow, 1)
                ' First paste the formats to avoid trouble mostly when pasting
                ' dates or time. Excel might firstly format it differently, and
                ' when pasting the formats might not revert to desired formats.
                .PasteSpecial xlPasteFormats
                .PasteSpecial xlPasteValues
                Application.CutCopyMode = False
            End With

        End If

    Next

    ' Go to the top of Target Worksheet.
    ActiveSheet.Range("A1").Select

    ' Inform user of success (Since the code is fast, you might not know if it
    ' had run at all).
    MsgBox "The operation finished successfully.", vbInformation, "Success"

ProcedureExit:

    ' Speed down.
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With

Exit Sub

ErrorHandler:

    MsgBox "An unexpected error occurred. Error '" & Err.Number & "': " _
            & Err.Description, vbCritical, "Error"
    GoTo ProcedureExit

End Sub