将工作表合并到一个合并工作表

时间:2018-02-08 11:29:16

标签: excel vba excel-vba

我正在寻找一种VBA功能,它允许将工作簿中的12张合并到名为“合并”的现有工作表13中。

每张纸都与第一行中的标题具有相同的结构。

12张纸上的数据按公式计算。我不想复制公式,也要复制单元格的值。

要复制的数据范围是A2:T(动态行数)

如果你能帮助我,我会非常感激

1 个答案:

答案 0 :(得分:1)

Sub ConsolidateWorksheets()

'Declare the Required Variables
Dim Wk As Workbook
Dim LastRow As Long
Dim LastRow2 As Long
Dim Check As String
Dim TotalSheets As Integer

'Assign the Declared Variables
Set Wk = ActiveWorkbook
TotalSheets = 12


'Use a For Loop to run over the required sheets
For i = 1 To TotalSheets 

    'Finding out the lastrow in each sheet to copy
    LastRow = Wk.Sheets(i).Range("A" & Rows.Count).End(xlUp).Row

    Wk.Sheets(i).Range("A2" & ":" & "T" & LastRow).Copy

    'Finding out the lastrow to write/paste
    LastRow2 = Wk.Sheets(13).Range("A" & Rows.Count).End(xlUp).Row

    'if the lastrow is not empty then write row will be lastrow + 1

    Check = Wk.Sheets(13).Range("A" & LastRow2).Value

    If Check <> "" Then
    LastRow2 = LastRow2 + 1
    End If

    'write the value in the consolidated sheet - sheet13
    Wk.Sheets(13).Range("A" & LastRow2).PasteSpecial xlPasteValues

Next

End Sub

如果有帮助,请接受答案。