如果列C标记为Y,如何将整行从一个工作表复制到另一个工作表

问题描述 投票:-3回答:1

我在工作簿中有三张纸,我想将所有前三张纸中的所有行复制到第4页,其中“C”列标记为Y.

excel vba
1个回答
0
投票

我正在回答这个问题,因为我很无聊。下面的代码指定工作表过滤到数组,循环通过数组,过滤范围,然后复制过滤器数据中的可见单元格。如果“A1”为空;它会将每个工作表中复制的数据粘贴到Range("A1"),否则它会将数据粘贴到“A列”中的第一个空单元格。根据需要更改工作表名称。

Sub FltrPste()
Dim wb As Workbook, shtArr As Variant, wsDest As Worksheet, i As Long, lRow4 As Long

Set wb = ThisWorkbook
shtArr = Array("Sheet1", "Sheet2", "Sheet3")
Set wsDest = wb.Sheets("Sheet4")
lRow = wsDest.Range("A" & Rows.Count).End(xlUp).Row

    For i = LBound(shtArr) To UBound(shtArr)
        With wb.Sheets(shtArr(i)).Range("A1").CurrentRegion
            .AutoFilter
            .AutoFilter Field:=3, Criteria1:="y"
            .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy
                If Range("A1") = "" Then
                    wsDest.Cells(1, 1).PasteSpecial xlPasteValues
                Else
                    wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                End If
                .AutoFilter
        End With
    Next i
End Sub 
© www.soinside.com 2019 - 2024. All rights reserved.