Excel- VBA过滤并移动结果

时间:2015-02-10 17:02:42

标签: excel-vba filter vba excel

我正在尝试设置vba代码来过滤一张数据并将包含特定值的行剪切到其他工作表。下面的代码段用于其中一个过滤器。到目前为止,我正在努力调试这条线。

myRange.Offset(1).Resize(myRange.Rows.Count - 1).SpecialCells(xlVisible).Cut 

我一直收到错误,我无法在多个部分运行此错误。任何帮助将不胜感激。

Sub MoveButton()
Sheets("Sheet1").Activate
Set myRange = Range(Range("A1"), Range("Z" & Cells.Rows.Count).End(xlUp))
myRange.Resize(1).AutoFilter
myRange.AutoFilter Field:=13, Criteria1:="Closed"
myRange.Offset(1).Resize(myRange.Rows.Count - 1).SpecialCells(xlVisible).Cut
Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Closed").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
myRange.AutoFilter

End Sub

好的,这就是我使用记录宏的原因。现在我只是不确定如何更改范围,以便他们自动选择所有过滤的行并将它们复制并粘贴到下一张纸上的下一个wmpty行。

Sub MoveCancelled()

    Sheets("Sheet1").Select
    Rows("2:2").Select
    Selection.AutoFilter
    Selection.AutoFilter
    Range("A2").Select
    ActiveWindow.ScrollColumn = 7
    ActiveSheet.Range("$A$2:$Z$20").AutoFilter Field:=13,Criteria1:="Closed"
    Rows(3).Select ' Selecting the row to copy, but it could be more than just one row.
    Selection.Copy
    Sheets("Closed").Select
    Rows(114).Select ' Selecting the paste location at the bottom of the next sheet.
    ActiveSheet.Paste


End Sub

我找到了这个帖子并最终废弃了我所有其他的努力。

Autofilter Macro, then copy visible data ONLY and paste to next available row

此解决方案运行良好。

1 个答案:

答案 0 :(得分:0)

我相信这是.cut操作让你感到悲伤,试试这个,

Sub MoveButton()
    Dim ws As Worksheet, MyRng As Range, rws As Long, Frng As Range
    Dim sh As Worksheet
    Set ws = Worksheets("Proposals")
    With ws
        rws = .Cells(Rows.Count, "A").End(xlUp).Row
        Set MyRng = .Range(.Cells(1, 1), .Cells(rws, "Z"))
        MyRng.AutoFilter Field:=13, Criteria1:="Closed"
        Set Frng = .Range(.Cells(2, 1), .Cells(rws, "Z")).SpecialCells(xlCellTypeVisible)
    End With

    Sheets.Add After:=Sheets(Sheets.Count)
    Set sh = ActiveSheet
    Frng.Copy sh.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
    Frng.EntireRow.Delete
    ws.AutoFilterMode = 0
End Sub