Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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,我正在尝试循环浏览工作簿中的所有工作表,查找每个工作表中最后使用的行,并将该行粘贴到名为aggregate的新工作表中。问题是它在循环时覆盖了工作表聚合中的行。我想复制第一个工作表的最后一行,将其粘贴到聚合。接下来,复制第二张工作表的最后一行,并将其粘贴到aggregate工作表中的下一行empty,依此类推。出于某种原因,我的代码没有递增聚合工作表中的下一个“空”行 代码: Sub-aggregate() ' '聚合宏 ' ' 将ws设置为工作表 最后一排一样长 Set wksDest=Act

我正在尝试循环浏览工作簿中的所有工作表,查找每个工作表中最后使用的行,并将该行粘贴到名为
aggregate
的新工作表中。问题是它在循环时覆盖了工作表
聚合中的行。我想复制第一个工作表的最后一行,将其粘贴到聚合。接下来,复制第二张工作表的最后一行,并将其粘贴到
aggregate
工作表中的下一行empty,依此类推。出于某种原因,我的代码没有递增
聚合
工作表中的下一个“空”行

代码:

Sub-aggregate()
'
'聚合宏
'
'
将ws设置为工作表
最后一排一样长
Set wksDest=ActiveWorkbook.Sheets(“聚合”)
对于工作表中的每个ws
如果ws.Name为“聚合”,那么
与ws
ws.Cells(Rows.Count,1).End(xlUp).EntireRow.Copy_
目标:=工作表(“聚合”).单元格(行数、计数、“A”).结束(xlUp).偏移量(1)
Application.CutCopyMode=False
以
如果结束
下一个ws
端接头

我花了两个小时才找到这个问题,但运气不好。请帮助。

您的代码看起来不应该像您描述的那样失败。然而,你似乎在方法中来回跳跃;e、 g.
set
wksDest而不声明变量,然后再也不使用它,可以将
与ws
块一起使用,但也不使用该块

这里是一个快速重写

Sub aggregateIt()
'
' aggregate Macro
'
    Dim ws As Worksheet, wksDest As Worksheet

    Set wksDest = ActiveWorkbook.Worksheets("aggregate")

    For Each ws In Worksheets
        If ws.Name <> wksDest.Name Then
            With ws
                .Cells(.Rows.Count, 1).End(xlUp).EntireRow.Copy _
                  Destination:=wksDest.Cells(.Rows.Count, "A").End(xlUp).Offset(1)
                Application.CutCopyMode = False
            End With
        End If
    Next ws
End Sub
Sub-aggregateIt()
'
'聚合宏
'
将ws设置为工作表,将WksTest设置为工作表
设置wksDest=ActiveWorkbook.Worksheets(“聚合”)
对于工作表中的每个ws
如果ws.Name wksDest.Name那么
与ws
.Cells(.Rows.Count,1).End(xlUp).EntireRow.Copy_
目的地:=wksDest.Cells(.Rows.Count,“A”).End(xlUp)。偏移量(1)
Application.CutCopyMode=False
以
如果结束
下一个ws
端接头

我还重命名了您的子过程,因为聚合是一个工作表函数。

您的代码看起来不像您描述的那样失败。然而,你似乎在方法中来回跳跃;e、 g.
set
wksDest而不声明变量,然后再也不使用它,可以将
与ws
块一起使用,但也不使用该块

这里是一个快速重写

Sub aggregateIt()
'
' aggregate Macro
'
    Dim ws As Worksheet, wksDest As Worksheet

    Set wksDest = ActiveWorkbook.Worksheets("aggregate")

    For Each ws In Worksheets
        If ws.Name <> wksDest.Name Then
            With ws
                .Cells(.Rows.Count, 1).End(xlUp).EntireRow.Copy _
                  Destination:=wksDest.Cells(.Rows.Count, "A").End(xlUp).Offset(1)
                Application.CutCopyMode = False
            End With
        End If
    Next ws
End Sub
Sub-aggregateIt()
'
'聚合宏
'
将ws设置为工作表,将WksTest设置为工作表
设置wksDest=ActiveWorkbook.Worksheets(“聚合”)
对于工作表中的每个ws
如果ws.Name wksDest.Name那么
与ws
.Cells(.Rows.Count,1).End(xlUp).EntireRow.Copy_
目的地:=wksDest.Cells(.Rows.Count,“A”).End(xlUp)。偏移量(1)
Application.CutCopyMode=False
以
如果结束
下一个ws
端接头

我还重命名了您的子过程,因为聚合是一个工作表函数。

如果您的问题源于某些行在列a中没有数据,那么使用
.Find
方法确定最后一行的宏应该很有用:

Option Explicit
Sub aggregate()
'
' aggregate Macro
'

'
 Dim ws As Worksheet ', wksDest As Worksheet
 Dim wsDest As Worksheet
 Dim c As Range, d As Range


 Set wsDest = ActiveWorkbook.Sheets("aggregate")


For Each ws In Worksheets
 If ws.Name <> "aggregate" Then

  With ws
    Set c = .Cells.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues, _
         searchorder:=xlByRows, searchdirection:=xlPrevious)
    With wsDest
            Set d = .Cells.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues, _
                searchorder:=xlByRows, searchdirection:=xlPrevious)
            If d Is Nothing Then Set d = .Cells(1, 1) 'check if wsDest is blank
    End With

    If Not c Is Nothing Then _
        c.EntireRow.Copy Destination:=d.Offset(1, 0).EntireRow
    Application.CutCopyMode = False
  End With
 End If
Next ws
End Sub
选项显式
次骨料()
'
'聚合宏
'
'
将ws设置为“工作表”,WksTest设置为“工作表”
将wsDest设置为工作表
尺寸c作为范围,d作为范围
设置wsDest=ActiveWorkbook.Sheets(“聚合”)
对于工作表中的每个ws
如果ws.Name为“聚合”,那么
与ws
设置c=.Cells.Find(what:=“*”,after:=.Cells(1,1),LookIn:=xlValues_
搜索顺序:=xlByRows,搜索方向:=xlPrevious)
用wsDest
Set d=.Cells.Find(what:=“*”,after:=.Cells(1,1),LookIn:=xlValues_
搜索顺序:=xlByRows,搜索方向:=xlPrevious)
如果d为空,则设置d=.Cells(1,1)'检查wsDest是否为空
以
如果不是,那么c什么都不是_
c、 EntireRow.Copy目标:=d.Offset(1,0).EntireRow
Application.CutCopyMode=False
以
如果结束
下一个ws
端接头

如果问题源于某些行在A列中没有数据,则此宏(使用
.Find
方法确定最后一行)应该很有用:

Option Explicit
Sub aggregate()
'
' aggregate Macro
'

'
 Dim ws As Worksheet ', wksDest As Worksheet
 Dim wsDest As Worksheet
 Dim c As Range, d As Range


 Set wsDest = ActiveWorkbook.Sheets("aggregate")


For Each ws In Worksheets
 If ws.Name <> "aggregate" Then

  With ws
    Set c = .Cells.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues, _
         searchorder:=xlByRows, searchdirection:=xlPrevious)
    With wsDest
            Set d = .Cells.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues, _
                searchorder:=xlByRows, searchdirection:=xlPrevious)
            If d Is Nothing Then Set d = .Cells(1, 1) 'check if wsDest is blank
    End With

    If Not c Is Nothing Then _
        c.EntireRow.Copy Destination:=d.Offset(1, 0).EntireRow
    Application.CutCopyMode = False
  End With
 End If
Next ws
End Sub
选项显式
次骨料()
'
'聚合宏
'
'
将ws设置为“工作表”,WksTest设置为“工作表”
将wsDest设置为工作表
尺寸c作为范围,d作为范围
设置wsDest=ActiveWorkbook.Sheets(“聚合”)
对于工作表中的每个ws
如果ws.Name为“聚合”,那么
与ws
设置c=.Cells.Find(what:=“*”,after:=.Cells(1,1),LookIn:=xlValues_
搜索顺序:=xlByRows,搜索方向:=xlPrevious)
用wsDest
Set d=.Cells.Find(what:=“*”,after:=.Cells(1,1),LookIn:=xlValues_
搜索顺序:=xlByRows,搜索方向:=xlPrevious)
如果d为空,则设置d=.Cells(1,1)'检查wsDest是否为空
以
如果不是,那么c什么都不是_
c、 EntireRow.Copy目标:=d.Offset(1,0).EntireRow
Application.CutCopyMode=False
以
如果结束
下一个ws
端接头

如果ws.Name“aggregate”,则
是区分大小写的比较。工作表的名称实际上是小写的吗?是的,工作表的名称是小写的。我无法在这里重现您的问题。可能是某些工作表中的某些行在A列中没有任何内容吗?我认为@RonRosenfeld也是这样,但是.End(xlUp)不会捕获该行,它会继续(从底部)查找A列中的第一个非空白单元格。@Jeeped Agree。但是我想他可能没有以我能理解的方式表达他所看到的内容。
如果ws.Name“aggregate”那么
是区分大小写的比较。工作表的名称实际上是小写的吗?是的,工作表的名称是小写的。我无法在这里重现您的问题。可能是某些工作表中的某些行在A列中没有任何内容吗?我认为@RonRosenfeld也是这样,但是.End(xlUp)不会捕获该行,它会继续查找(从