将来自不同工作表的数据合并到主工作表

时间:2017-06-25 15:44:02

标签: excel-vba vba excel

提前感谢您的帮助。我编写了一个代码,可以将第68行向前复制到最后一个填充单元格(在A列中)来自不同的工作表,然后粘贴到一个名为" Conso"从第3排。我希望手动复制主表中的标题字段。但是它给出了运行时错误1004应用程序定义或对象定义的错误而没有给出任何结果。能不能帮助我,因为我不是专家。

Sub MergeDataFromWorksheets()
  Dim sh As Worksheet
  Dim DestSh As Worksheet
  Dim erow As Long, lrowsh As Long, startrow As Long
  Dim CopyRng As Range
  startrow = 68
  With Application
    .ScreenUpdating = False
    .EnableEvents = False
  End With
  Set DestSh = ActiveWorkbook.Worksheets("Conso")
  If IsEmpty(Sheets("Conso").Range("A3:BT400")) = False Then
    Sheets("Conso").Range("A3:BT400").ClearContents
  End If
  For Each sh In ActiveWorkbook.Worksheets
    If sh.Name <> "Conso" And sh.Name <> "Data" And sh.Name <> "Template" Then
      erow = DestSh.Range("A" & Rows.count).End(xlUp).Offset(1, 0).row
      lrowsh = sh.Range("A" & Rows.count).End(xlUp).row
      Set CopyRng = sh.Range(sh.Rows(startrow), sh.Rows(lrowsh))
      CopyRng.Copy
      Sheets("Conso").Range(erow, 1).PasteSpecial Paste:=xlPasteFormats
      Application.CutCopyMode = False
    End If
  Next
  With Application
    .ScreenUpdating = True
    .EnableEvents = True
  End With
End Sub

0 个答案:

没有答案