将所有数据合并到新工作表中,不包括第一张工作表

时间:2017-07-14 07:05:27

标签: excel-vba vba excel

我的工作簿中有4张。我想在新工作表中组合所有数据。我得到了下面写的代码。但现在我不想在新表中显示sheet1数据。附上工作表供您参考。在此先感谢!!!!

          sub Combine()
          Dim J As Integer
          On Error Resume Next
          Sheets(1).Select
          Worksheets.Add
          Sheets(1).Name = "Combined"
          Sheets(2).Activate
          Range("A1").EntireRow.Select
          Selection.Copy Destination:=Sheets(1).Range("A1")
          For J = 2 To Sheets.Count
          Sheets(J).Activate
          Range("A1").Select
          Selection.CurrentRegion.Select
          Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
          Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
          Next
          End Sub

1 个答案:

答案 0 :(得分:0)

只需对代码进行细微更改即可:

        Sub Combine()
          Dim Lastrow As Integer
          Dim J As Integer
          On Error Resume Next
          Sheets(1).Select
          Worksheets.Add
          Sheets(1).Name = "Combined"
          Sheets(3).Activate
          Range("A1").EntireRow.Select
          Selection.Copy Destination:=Sheets(1).Range("A1")
          For J = 3 To Sheets.Count
            Sheets(J).Activate
            ' First delete the empty rows
            Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
            Range("A2:L" & Lastrow).Select
            Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            ' Then select the region as a table
            Range("A1").Select
            Selection.CurrentRegion.Select
            Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
            Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
          Next
        End Sub