如何从一张纸上复制过滤后的数据并粘贴到另一张纸上?

时间:2017-05-29 06:09:17

标签: excel excel-vba vba

我已经输入了autofilter代码来指定copypaste操作的标准,但不确定我是否正确完成了。

Sub test3()

Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long
Dim strSearch As String


Set ws1 = Worksheets("sheet1")

strSearch = "LOCAL"

With ws1

    '~~> Remove any filters
    .AutoFilterMode = False


    lRow = .Range("L" & .Rows.Count).End(xlUp).Row

    With .Range("L4:L" & lRow)
        .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
        Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
    End With

    '~~> Remove any filters
    .AutoFilterMode = False
End With

'~~> Destination File

Set ws2 = Worksheets("Sheet2")

With ws2
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
        lRow = .Cells.Find(What:="*", _
                      After:=.Range("A1"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False).Row
    Else
        lRow = 1
    End If

    copyFrom.Copy .Rows(lRow)
End With

Worksheets("Sheet2").Columns().AutoFit
Cells(1, 1).Activate

End Sub

非常感谢有人能突出显示我使用的代码出了什么问题。 感谢。

0 个答案:

没有答案