Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将所有数据合并到新工作表中,不包括第一个工作表_Excel_Vba - Fatal编程技术网

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

Excel 将所有数据合并到新工作表中,不包括第一个工作表,excel,vba,Excel,Vba,我的工作簿中有4张工作表。我想将所有数据合并到新工作表中。我得到了我写在下面的代码。但现在我不想在新工作表中显示sheet1数据。已附上工作表供您参考。提前感谢 sub Combine() Dim J As Integer On Error Resume Next Sheets(1).Select Worksheets.Add Sheets(1).Name = "Combine

我的工作簿中有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

只有对代码进行微小的更改,才能实现此功能:

        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

很荣幸!我的一些工作表的数据之间有间隙(1个或多个空行)。而上面的代码只获取连续数据。我需要将整个数据合并到工作表中。请做需要的事。谢谢您好,我编辑的代码考虑到了空行,基本上选择所有数据并删除空行,然后选择数据区域(如带标题的表)。这是一种乐趣!