我想将已过滤的行复制到另一个工作表,但在下面的代码中收到错误。我在E栏中有三个状态,即打开,关闭和逾期。我想复制状态为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
好吧,因为我是新手,我没有说出错误是什么,输入工作簿来自哪里......所以也许我的用户给我生病的格式化的东西,应该是没有过滤器的。 ..
答案 0 :(得分:0)
使用您的代码,我得到了通常的
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