首页 > 解决方案 > 将过滤的单元格范围从一个工作表复制到另一个工作表的最后一行

问题描述

我正在尝试在 1 个工作表(“2。选择研究特定表格”)上过滤一系列数据(通过使用 L 列中的“是”)并将数据复制到另一个工作表的最后一行(“3。工作表” )。我正在使用下面的代码。到目前为止,代码过滤了第一个工作表上的数据,但数据不会复制到第二个工作表。

提前感谢您的任何帮助。

 Sub FilterAndCopy2()

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual


    Dim LastRow As Long, LastRow2 As Long
    Dim TARGetSheet As Worksheet, TKSheet As Worksheet

    Set TARGetSheet = Sheets("2. Select Study-Specific Forms") 'Set sheet where filtered data is
    Set TKSheet = Sheets("3. Worksheet") ' Set Sheet name to copy data to

    LastRow = TARGetSheet.Range("L" & Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
    LastRow2 = TKSheet.Range("A" & Rows.Count).End(xlUp).Row + 1 ' Determine the next empty row in order to paste the data


    With TARGetSheet
        .AutoFilterMode = False

        With .Range("G7", "L" & LastRow)
        .AutoFilter
        .AutoFilter Field:=6, Criteria1:="Yes"
        End With
    End With

     TARGetSheet.Range("G7", "L" & LastRow).Copy
     TKSheet.Range("A" & LastRow2).PasteSpecial (xlPasteAll)

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic

    End Sub

标签: excelvba

解决方案


推荐阅读