我有三张名为:" Dane magazyn"," Sheet3"和" Dostawcy"。 我希望我的Excel做的是:
1)过滤掉col中的#N / A值。表格中的J" Dane magazyn"。过滤后应保留的所有值都存储在表格中的第E栏中;#34; Dostawcy" - 21个条目,但将来会更多。
2)选择过滤后剩余的数据并复制到" Sheet3"
到目前为止,这是我的代码:
Sub filtruj()
Dim i As Long, arr As Variant, LastRow As Long
Sheets("Dostawcy").Select
With ActiveSheet
LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
End With
arr = Sheets("Dostawcy").Range("E2:E" & LastRow).Value
Sheets("Dane magazyn").Select
With ActiveSheet
**.Columns("J").AutoFilter Field:=1, Criteria1:=arr, Operator:=xlFilterValues** <---- here I get error
End With
其余代码......
我得到的错误信息是: &#34;运行时错误&#39; 1004&#39;:
Range类的AutoFilter方法失败&#34;
我检查过的网站(并非全部列出)
Using string array as criteria in VBA autofilter
VBA assign a range to an Array from different sheet
Fastest way to read a column of numbers into an array
提前致谢
答案 0 :(得分:0)
这是工作代码:
Dim rng, rngToFilter As Range
Dim i As Integer: i = 1
'set you range to area with values to compare against
'if you can, specify here exact range instead of whole column, it can increase efficiency
Set rng = Sheets("Dostawcy").Range("E:E")
'set range to be filtered out, don't specify here whole column,
'in following loop it can result in overflow error
Set rngToFilter = Sheets("Dane magazyn").Range("J1:J100")
'here we will iterate through all cells within the searched range,
'for every cell we will try to find it's value within the other range,
'if it succeeds, so it's not Nothing, then we copy it to Sheet3
For Each cell In rngToFilter
'if any cell causes the error, we will skip one iteration
On Error GoTo ErrorHandler:
If Not rng.Find(cell.Value) Is Nothing Then
Sheets("Sheet3").Cells(i, 1).Value = cell.Value
i = i + 1
End If
ErrorHandler:
Next cell
除非必须,否则不要使用Select
,否则会降低程序的效率。