简体   繁体   English

将筛选的行复制到另一个工作表

[英]Copy filtered rows to another sheet

I want to copy the filtered row to another sheets but getting an error on below code. 我想将已过滤的行复制到另一个工作表,但在下面的代码中收到错误。 I have three status in column E ie Open, Close and Overdue. 我在E列中有三个状态,即Open,Close和Overdue。 I want to copy all the rows having status as open or overdue. 我想复制状态为open或overdue的所有行。

Error occurred at the below line 下面的行发生错误

.AutoFilter Field:=5, Criteria1:="open"

Here's the code 这是代码

Dim Wb As Workbook
Dim ws As Worksheet
Set Wb = Workbooks.Open(TextBox2.Text)
Set ws = Wb.Sheets(strSheetName)

With ws
    .AutoFilterMode = False
    With .Range("A1:E65536")
        .AutoFilter Field:=5, Criteria1:="open"
        .SpecialCells(xlCellTypeVisible).copy Destination:=Sheets("Master Records").Range("A1")
    End With
End With

Well, since I'm new I didn't say what was the error, nor where the input workbook where coming from... So maybe my users give me ill formatted stuff, without filter in the sheet that should be... 好吧,因为我是新手,我没有说出错误是什么,输入工作簿来自哪里......所以也许我的用户给了我格式化的东西,没有过滤器应该是...

With your code, I get the usual 使用您的代码,我得到了通常的

 Run-time error '1004' 

But more specifically 但更具体地说

  AutoFilter method of Range class failed 

Because there was no filter in my sheet. 因为我的工作表中没有过滤器。

So here is to add the filter for the sheet: 所以这里是为工作表添加过滤器:

Sub Macro3()
  Dim Wb As Workbook
  Dim ws As Worksheet
  Set Wb = ThisWorkbook
  Set ws = Wb.Sheets("Sheet1")

  ' here is what to add:      
  Range("A1:E1").Select
  Selection.AutoFilter
  ' done

  With ws
      .AutoFilterMode = False
      With .Range("A1:E65536")
          .AutoFilter Field:=6, Criteria1:="open"
          .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("Master Records").Range("A1")
...
      End With
  End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM