我已经在VBA中创建了代码,用于过滤传输前的数据,但我想添加一个额外的过滤器。
我该怎么做?
我尝试仅在条件1下使用新值添加一行,但是它不起作用。
With Wb1.Sheets(12)
If .AutoFilterMode Then .AutoFilterMode = False
.Range("$A$1:$AM" & .Cells(.Rows.Count, "A").End(xlUp).Row).AutoFilter Field:=19, _
Criteria1:="Test,Test"
Set rngToCopy = .AutoFilter.Range
End With
rngToCopy.Copy
wb2.Sheets(2).Range("$A1").PasteSpecial xlValues
End If
ThisWorkbook.RefreshAll
由于某些原因仍然有问题,这里是完整的代码。当我执行时,我没有收到错误,但是没有任何副本。
Sub CopyData()
On Error GoTo ErrorHandle
Application.ScreenUpdating = False
Dim Wb1 As Workbook, wb2 As Workbook, wB As Workbook
Dim rngToCopy As Range
For Each wB In Application.Workbooks
If Left(wB.Name, 21) = "Test_Test Test" Then
Set Wb1 = wB
Exit For
End If
Next
If Not Wb1 Is Nothing Then '<~~ check if you actually found the needed workbook
Set wb2 = ThisWorkbook
With Wb1.Sheets(12).Range("$A$1:$AM" & Cells(Rows.Count, "A").End(xlUp).Row)
If AutoFilterMode Then .AutoFilterMode = False
.AutoFilter Field:=19, Criteria1:="Test,Test"
.AutoFilter Field:=38, Criteria1:="Employee"
'do stuff
Set rngToCopy = .AutoFilter.Range
End With
rngToCopy.Copy
wb2.Sheets(2).Range("$A1").PasteSpecial xlValues
End If
ThisWorkbook.RefreshAll
答案 0 :(得分:0)
要在单个列中过滤多个条件的范围,请使用Or
...
With Wb1.Sheets(12).Range("$A$1:$AM" & Cells(Rows.Count, "A").End(xlUp).Row)
.AutoFilter Field:=19, Criteria1:="Test,Test", Operator:=xlOr, Criteria2:="No"
'Do stuff
End With
要在一个列中过滤两个以上的条件,必须使用一个数组...
Criteria1:=Array("Test,Test", "No", "Yes")
要为不同列中的多个条件过滤范围(您必须设置两个字段)...
With Wb1.Sheets(12).Range("$A$1:$AM" & Cells(Rows.Count, "A").End(xlUp).Row)
If AutoFilterMode Then .AutoFilterMode = False
.AutoFilter Field:=19, Criteria1:="Test,Test"
.AutoFilter Field:=38, Criteria1:="Employee"
'do stuff
End With