簡體   English   中英

將篩選的行復制到另一個工作表

[英]Copy filtered rows to another sheet

我想將已過濾的行復制到另一個工作表,但在下面的代碼中收到錯誤。 我在E列中有三個狀態,即Open,Close和Overdue。 我想復制狀態為open或overdue的所有行。

下面的行發生錯誤

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

這是代碼

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

好吧,因為我是新手,我沒有說出錯誤是什么,輸入工作簿來自哪里......所以也許我的用戶給了我格式化的東西,沒有過濾器應該是...

使用您的代碼,我得到了通常的

 Run-time error '1004' 

但更具體地說

  AutoFilter method of Range class failed 

因為我的工作表中沒有過濾器。

所以這里是為工作表添加過濾器:

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