自动过滤器有3列

时间:2016-10-27 20:04:45

标签: excel vba excel-vba

我目前有代码来过滤3列中的特定项目。自动过滤器是否有限制,只允许过滤2列?一旦我添加第3列过滤器,我得到“范围类失败的自动过滤方法”错误。如果有限制,是否有任何解决方法?

    Public Sub autofilterOffical()
Dim ws As Worksheet
Dim wsOut As Worksheet
Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim e As String
Dim f As String
Dim g As String
Dim h As String
Dim i As String
Dim j As String
Dim k As String
Dim l As String
Dim m As String



Set ws = Worksheets("Data")
AutoFilterMode = False
a = ws.Range("bq6")
b = ws.Range("bq7")
c = ws.Range("bq8")
d = ws.Range("bq9")
e = ws.Range("bq10")
f = ws.Range("bq11")
g = ws.Range("bq12")
h = ws.Range("bq13")
i = ws.Range("bq14")
j = ws.Range("bq15")
k = ws.Range("bq16")
l = ws.Range("bq17")
m = ws.Range("bq18")

If ws.Range("br18") = "1" Then
ws.Range("A:Bn").autofilter field:=66, Criteria1:=Array(m), _
Operator:=xlFilterValues
Else
End If


If ws.Range("br6") = "1" Then
ws.Range("a:bn").autofilter field:=64, Criteria1:=Array(a), _
Operator:=xlFilterValues
ElseIf ws.Range("br6") = "2" Then
ws.Range("a:bn").autofilter field:=64, Criteria1:=Array(a, b), _
Operator:=xlFilterValues
ElseIf ws.Range("br6") = "3" Then
ws.Range("a:bn").autofilter field:=64, Criteria1:=Array(a, b, c), _
Operator:=xlFilterValues
ElseIf ws.Range("br6") = "4" Then
ws.Range("a:bn").autofilter field:=64, Criteria1:=Array(a, b, c, d), _
Operator:=xlFilterValues
ElseIf ws.Range("br6") = "5" Then
ws.Range("a:bn").autofilter field:=64, Criteria1:=Array(a, b, c, d, e), _
Operator:=xlFilterValues
ElseIf ws.Range("br6") = "6" Then
ws.Range("a:bn").autofilter field:=64, Criteria1:=Array(a, b, c, d, e, f), _
Operator:=xlFilterValues
ElseIf ws.Range("br6") = "7" Then
ws.Range("a:bn").autofilter field:=64, Criteria1:=Array(a, b, c, d, e, f, g), _
Operator:=xlFilterValues
ElseIf ws.Range("br6") = "8" Then
ws.Range("a:bn").autofilter field:=64, Criteria1:=Array(a, b, c, d, e, f, g, h), _
Operator:=xlFilterValues
ElseIf ws.Range("br6") = "9" Then
ws.Range("a:bn").autofilter field:=64, Criteria1:=Array(a, b, c, d, e, f, g, h, i), _
Operator:=xlFilterValues
ElseIf ws.Range("br6") = "0" Then

Else
End If

If ws.Range("br15") = "1" Then
ws.Range("a:bn").autofilter field:=63, Criteria1:=Array(j), _
Operator:=xlFilterValues
ElseIf ws.Range("br15") = "2" Then
ws.Range("a:bn").autofilter field:=63, Criteria1:=Array(j, k), _
Operator:=xlFilterValues
ElseIf ws.Range("br15") = "3" Then
ws.Range("a:bn").autofilter field:=63, Criteria1:=Array(j, k, l), _
Operator:=xlFilterValues
Else
End If




Call copy


End Sub

1 个答案:

答案 0 :(得分:2)

您的第三个过滤器有时(通常?通常?)失败,因为您尝试过滤该范围的第66列。

但是,如果BR6在1到9之间,那么您已将过滤器范围定义为A:BL,其宽度仅为64列。

如果BR6不在1到9之间,但BR15介于1和3之间,那么您已将滤波器范围定义为A:BK,其宽度仅为63列。

最终过滤器的唯一方法是,如果您没有执行前两个过滤器中的任何一个,因为在这种情况下,您将范围设置为A:BQ

我建议您在所有情况下将过滤器设置为A:BN列。