如何基于文本值在工作表之间复制行

时间:2019-06-17 06:55:51

标签: excel vba

我试图根据行H列中特定文本值(“是”)的出现,将整个行从一个工作表复制到另一工作表。

我找到了此代码,并希望对其进行更改以搜索工作簿中的多个工作表。我已经读过关于使用数组的信息,但是我不确定如何实现它。

如果需要更改前1000行,则无需检查,整个工作表都很好。

谢谢。

#if

1 个答案:

答案 0 :(得分:3)

我只过滤您的范围,然后像这样复制过滤后的数据:

Option Explicit
Sub CopyYes()

    Dim LastRow As Long, Col As Long, Lrow As Long
    Dim Source As Worksheet, Target As Worksheet
    Dim arrws
    Dim HandleIt As Variant

    ' Change worksheet designations as needed
    Set Target = ThisWorkbook.Worksheets("Storage")

    arrws = Array("Jan 19", "Feb 19") 'add all the worksheets you need to loop through

    For Each Source In ThisWorkbook.Worksheets

        HandleIt = Application.Match(Source.Name, arrws, 0)
        If Not IsError(HandleIt) Then
            With Source
                .UsedRange.AutoFilter Field:=8, Criteria1:="yes"
                LastRow = .Cells(.Rows.Count, "H").End(xlUp).Row
                Col = .Cells(1, .Columns.Count).End(xlToLeft).Column
                Lrow = Target.Cells(Target.Rows.Count, 1).End(xlUp).Row + 1
                .Range("A2", .Cells(LastRow, Col)).SpecialCells(xlCellTypeVisible).Copy Target.Range("A" & Lrow)
            End With
        End If
    Next Source

End Sub

您将一口气获得相同的输出,从而避免了循环。