简体   繁体   English

VBA 上的 2 个标准自动过滤器存在问题

[英]Problem with 2 criteria autofilter on VBA

I m trying to filter the dates in a column using autofilter, but when i must filter between two dates this error shows: "The autofilter method of the range class has failed". Im trying to filter the dates in a column using autofilter, but when i must filter between two dates this error shows: "The autofilter method of the range class has failed". I m trying to filter the dates in a column using autofilter, but when i must filter between two dates this error shows: "The autofilter method of the range class has failed". I m getting the date values from two text boxes in the activesheet. m trying to filter the dates in a column using autofilter, but when i must filter between two dates this error shows: "The autofilter method of the range class has failed". I从活动表中的两个文本框中获取日期值。

I`ve alreary tried to dim a listobject and do something like "listobject.range.autofilter", but got the same error.我已经尝试使列表对象变暗并执行“listobject.range.autofilter”之类的操作,但遇到了同样的错误。

Private Sub DataDe_Change()

'dim var
Dim rng As Range

'set var
Set rng = Range("B2:T2")

'if both text boxes are empty the filter clears
If DataDe.Text = "" And DataAte.Text = "" Then
    rng.AutoFilter Field:=1
End If

'if the value of this textbox isn't a date the sub won't do anything
If Not IsDate(DataDe.Text) Then GoTo subkill:

'if it is a date then it cheks if the other textbox
'contains a date and apply an one criteria filter
If Not IsDate(DataAte.Text) Then
    rng.AutoFilter Field:=1, _
            Criteria1:=">=" & Format(DataDe.Text, "dd/mm/yyyy")
End If

'if both are dates apply a two criteria filter
'and only in this condition the error pops up
If IsDate(DataAte.Text) Then
    rng.AutoFilter Field:=1, _
            Criteria1:=">=" & Format(DataDe.Text, "dd/mm/yyyy"), _
            Operator:=x1And, _
            Criteria2:="<=" & Format(DataAte.Text, "dd/mm/yyyy")
End If


subkill:

End Sub

This is how I tried to recreate the problem这就是我试图重现问题的方式

Private Sub TextBox2_Change()

    Dim rng As Range

    Set rng = Range("A1:B1")

    If Me.TextBox2.Text = "" And Me.TextBox1.Text = "" Then
        rng.AutoFilter 1
    End If

    If Not IsDate(Me.TextBox2.Text) Then GoTo subkill

    If Not IsDate(Me.TextBox1.Text) Then
        rng.AutoFilter 1, ">=" & Format(Me.TextBox1.Text, "m/d/yyyy")
    End If

    If IsDate(Me.TextBox2.Text) Then
        rng.AutoFilter 1, ">=" & Format(Me.TextBox1.Text, "m/d/yyyy"), xlAnd, "<=" & Format(Me.TextBox2.Text, "m/d/yyyy")
    End If

subkill:

End Sub

过滤列表对象

When I moved my range to B1:C6 and change the code to Set rng = Range("B1:C1") it still works.当我将范围移动到 B1:C6 并将代码更改为Set rng = Range("B1:C1")它仍然有效。 Generally this error means you're filtering on a column that's not in your range, but I don't see that here.通常,此错误意味着您正在过滤不在您范围内的列,但我在这里看不到。

I did read elsewhere that dates must be in US format for autofilter.我确实在其他地方读到日期必须是美国格式的自动过滤器。 If that's true, try changing your date format.如果是这样,请尝试更改您的日期格式。 But if you tried 1/1/2019 in both boxes, that's the same valid date in both formats.但是,如果您在两个框中都尝试了 2019 年 1 月 1 日,那么这两种格式的有效日期都是相同的。

I've finally found the problem.我终于找到了问题所在。 This code was not written entirely by me, I'm helping a friend on this task and when he sent it to me i didn't wrote my own code.该代码并非完全由我编写,我正在帮助一位朋友完成此任务,当他将其发送给我时,我并没有编写自己的代码。 The thing is, in the choosen font for the vba editor the "1" and the "l" are realy similar lol.问题是,在为 vba 编辑器选择的字体中,“1”和“l”非常相似,哈哈。 The "xland" operator was written as "x1and". “xland”运算符写为“x1and”。 It took me only 10 days to notice this:) Thanks for the help!我只用了 10 天就注意到了这一点:)感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM