复制每张纸的所有行

时间:2019-05-07 13:56:04

标签: excel vba

我需要代码方面的帮助。我要合并几张纸中的所有行,但Excel中一张纸中的标题除外。

这是代码:

Dim ws As Worksheet
Dim sh As Worksheet
Set sh = Sheets("P&L_consolidation")

For Each ws In Sheets
    If ws.Name <> "Zero's" Then
       ws.Range("A2", ws.Range("U"& Rows.Count).End(xlUp)).Copy sh.Range("A"& Rows.Count).ENd(xlUp)(2)
    End if
Next ws

如果工作表中包含一些数据,则此代码有效,但问题是如果某个工作表仅包含标题,则此代码复制该标题并将其粘贴到合并的工作表中。在那种情况下,我只想跳过该表。

请,有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

类似的事情应该对您有用:

Sub tgr()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim wsDest As Worksheet

    Set wb = ActiveWorkbook
    Set wsDest = wb.Worksheets("P&L_consolidation")

    For Each ws In wb.Worksheets
        If ws.Name <> "Zero's" Then
            With ws.Range("A2", ws.Cells(ws.Rows.Count, "U").End(xlUp))
                If .Row >= 2 Then .Copy wsDest.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
            End With
        End If
    Next ws

End Sub