简体   繁体   English

Excel VBA-日期过滤器和空白

[英]Excel VBA - Date Filter AND blanks

I am using VBA to make date filters. 我正在使用VBA制作日期过滤器。 This filter will take the dates I specify in sheet 1 to and filter the column I have selected at the moment. 此过滤器会将我在工作表1中指定的日期作为过滤条件,并过滤我目前选择的列。 Ideally, I would like to have all the values with the date in that range PLUS all blanks (where no date has been defined). 理想情况下,我希望将日期范围内的所有值加上所有空白(未定义日期)。

Set rep= ActiveWorkbook.Sheets("sheet2")
Set sh1 = ActiveWorkbook.Sheets("Sheet1")

Dim anf As String
Dim ende As String
Dim count As Integer

anf = ">=" + sh1.Range("J2")
ende = "<=" + sh1.Range("J3")
rep.Select
count = Range(Selection, Selection.End(xlToLeft)).Columns.count

rep.Range("$A$4:$GD$11668").AutoFilter Field:=count, Criteria1:=anf, Operator:=xlAnd, Criteria2:=ende, Operator _
        :=xlFilterValues

This code functions beautifully. 该代码功能精美。 However, it only filters the date. 但是,它仅过滤日期。 My attempts to add blanks as well (in addition to it) have failed. 我添加空白的尝试也失败了。

For example, adding a 3rd criterion for blanks: 例如,为空白添加第三个条件:

rep.Range("$A$4:$GD$11668").AutoFilter Field:=count, Criteria1:=anf, Operator:=xlAnd, Criteria2:=ende, Operator:=xlAnd, Criteria3:="=", Operator _
        :=xlFilterValues

I get an application defined or object defined error. 我收到一个应用程序定义或对象定义的错误。

Any ideas? 有任何想法吗? Thanks! 谢谢!

You can't do more than 3 criteria at the same time that way -if you notice in the user interface you can only get 2 "and" or whatever criteria at the same time. 这样一来,您不能同时执行3个以上的条件-如果您在用户界面中注意到,您只能同时获得2个“和”或任何其他条件。

在此处输入图片说明

However, you can do an array and set that as criteria. 但是,您可以创建一个数组并将其设置为条件。 Refer to this example 参考这个例子

Try the following example 试试下面的例子 在此处输入图片说明

Sub Sample()
Dim SampleRange As Range
Dim ArrayDates(2) As Date
ArrayDates(0) = "1-1-2017"
ArrayDates(1) = "1-2-2017"
ArrayDates(2) = Now()
'1st approach, add the array directly in the criteria
Set SampleRange = Range("A1:A31")
    SampleRange.AutoFilter Field:=1, Operator:= _
        xlFilterValues, Criteria1:=Array(2, "1/1/2017", 2, "1/2/2017", 2, "1/3/2017")
 '2nd approach: define an array and just start to call it as needed
        SampleRange.AutoFilter Field:=1, Operator:= _
        xlFilterValues, Criteria1:=Array(ArrayDates(1))

End Sub

First off, your error was caused by trying to give Autofilter 3 criteria - it can only handle 2, and it had no idea what to do with the extra arguments you passed it. 首先,您的错误是由于尝试给自动Autofilter 3提供条件引起的-它只能处理2,并且不知道如何处理传递给它的多余参数。

Now, that's not the only problem with what you're trying. 现在,这并不是您尝试的唯一问题。 Notice that if you attempt to filter between two dates manually through the UI, you can do it, but you can't also add blanks. 请注意,如果您尝试通过UI手动在两个日期之间进行过滤,则可以执行此操作,但是也不能添加空格。 It's kind of a one-thing-or-the-other situation. 这是一种或另一种情况。 I also tried Brian's solution at first, but Excel just doesn't work that way. 一开始我也尝试过Brian的解决方案,但Excel不能那样工作。 At least, I couldn't coax it into working that way. 至少,我无法哄骗它那样工作。

This leaves us with two very ugly possible solutions. 这给我们提供了两个非常丑陋的解决方案。 Either you modify the table you're filtering (add an extra column with a formula or something along those lines) to give Autofilter something else to filter on, or manually set all the valid criteria. 您可以修改要过滤的表(添加带有公式的额外列或沿这些行添加内容)以为自动过滤提供其他过滤条件,或者手动设置所有有效条件。 Here's how you do the latter: 后者的处理方法如下:

Private Sub setFilter()
    Set rep = ActiveWorkbook.Sheets("sheet2")
    Set sh1 = ActiveWorkbook.Sheets("Sheet1")

    Dim anf As String
    Dim ende As String
    Dim count As Integer

    anf = sh1.Range("J2")
    ende = sh1.Range("J3")
    rep.Select
    count = Range(Selection, Selection.End(xlToLeft)).Columns.count

    rep.Range("$A$4:$GD$11668").AutoFilter Field:=count, Criteria1:=GetValidDates(CDate(anf), CDate(ende)), Operator:=xlFilterValues
End Sub

Private Function GetValidDates(ByVal startD As Date, ByVal endD As Date) As String()
    Dim numDays As Integer: numDays = endD - startD + 1
    Dim dateArray() As String
    ReDim dateArray(numDays) ' (numDays + 1) entries

    Dim i As Integer
    For i = 0 To numDays - 1
        dateArray(i) = CStr(startD + i)
    Next
    dateArray(i) = ""

    GetValidDates = dateArray
End Function

This builds an array of all dates between and including the start and end (as well as an extra empty "" entry to allow blanks). 这将构建一个包含开始和结束之间(包括开始和结束)的所有日期的数组(以及一个额外的空“”条目以允许空白)。 It's ugly and I'm not sure how well it'll work for really big date ranges (might be slow), but at least Excel recognizes and accepts it. 这很丑陋,我不确定在很大的日期范围内(可能会很慢)它的效果如何,但是至少Excel可以识别并接受它。

I couldn't follow what you were trying to do, but maybe something like this: 我无法遵循您的尝试,但也许是这样的:

Sub test1()

Dim rep As Worksheet
Dim sh1 As Worksheet

Set rep = ActiveWorkbook.Sheets("sheet4")
Set sh1 = ActiveWorkbook.Sheets("Sheet3")

Dim i As Long
Dim count As Integer
Dim arr As Variant

arr = Array("=" & """""", ">=" & sh1.Range("J2"), "<=" & sh1.Range("J3"))

rep.Select
count = Range(Selection, Selection.End(xlToLeft)).Columns.count
For i = 0 To UBound(arr)
    rep.Range("$A$4:$GD$11668").AutoFilter Field:=count, Criteria1:=arr(i)
Next i

End Sub

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

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