简体   繁体   English

Excel 自动筛选条件数组循环

[英]Excel Autofilter Criteria array loop

I need to filter a column by dates.我需要按日期过滤一列。 I determine the number of dates needed, and then apply the filter.我确定所需的日期数,然后应用过滤器。 Originally I figured there were only 2 cases, Tuesday thru Friday where you use the previous date.最初我认为只有 2 种情况,即周二到周五,您使用前一个日期。 And Monday where Friday, Saturday and Sunday are needed.周一需要周五、周六和周日。

Then I realized that there might be a holiday on Friday or Monday, and it would require more dates...and there might be a midweek holiday, like Thanksgiving.然后我意识到周五或周一可能会有假期,这需要更多的日期……而且可能会有周中假期,比如感恩节。 I then came up with this to cover the 4 most common possibilities:然后我想出了这个来涵盖 4 种最常见的可能性:

  dPosting = dLast + 1 'dLast is the last Date used in the filter
  Select Case Date - dLast
    Case 2: 'Regular T - F (1 day)
      shFF.Range("A1", shFF.Cells(lMax, lCol)).AutoFilter Field:=cFind.Column, Operator:=xlFilterValues, Criteria1:=Array(2, dPosting)
    Case 3: 'Midweek Holiday (2 days)
      shFF.Range("A1", shFF.Cells(lMax, lCol)).AutoFilter Field:=cFind.Column, Operator:=xlFilterValues, Criteria1:=Array(2, dPosting, 2, dPosting + 1)
    Case 4: 'Regular Monday (3 days)
      shFF.Range("A1", shFF.Cells(lMax, lCol)).AutoFilter Field:=cFind.Column, Operator:=xlFilterValues, Criteria1:=Array(2, dPosting, 2, dPosting + 1, 2, dPosting + 2)
    Case 5: 'After F or M Holiday (4 days)
      shFF.Range("A1", shFF.Cells(lMax, lCol)).AutoFilter Field:=cFind.Column, Operator:=xlFilterValues, Criteria1:=Array(2, dPosting, 2, dPosting + 1, 2, dPosting + 2, 2, dPosting + 3)
    Case Else: 'Other
      MsgBox "Too many days since last run.  Please contact me", vbCritical, "Out of Scope"
  End Select

Then I figured, what if the person that runs this is out for a week for vacation...and threw in the Case Else .然后我想,如果运行它的人休假一周……然后扔进Case Else怎样。

Now I think that the easiest and cleanest way will be to simply determine the number of dates needed from the last date that was run dLast and Now.现在我认为最简单和最干净的方法是简单地确定从运行dLast和 Now 的最后一个日期开始所需的日期数。 Put that into an array and use that as the Criteria...but I can't figure out the syntax.将它放入一个数组并将其用作标准......但我无法弄清楚语法。

Checked these, but still but can't make it work:检查了这些,但仍然无法使其工作:

This is my test code to play with the syntax and data sheet:这是我使用语法和数据表的测试代码:

Sub Test()
Dim i As Integer
Dim d As Date
Dim sArr(0 To 2) As String

  d = #4/25/2016#
  For i = 0 To 2
    sArr(i) = "2, """ & d + i & """"
  Next i
  ActiveSheet.Range("$A$1:$R$16643").AutoFilter Field:=17, Operator:=xlFilterValues, Criteria1:=sArr

End Sub

The macro recorder spits this out for using 3 dates:宏记录器使用 3 个日期对此进行了说明:

    ActiveSheet.Range("$A$1:$R$16643").AutoFilter Field:=17, Operator:=xlFilterValues, _
Criteria2:=Array(2, "4/25/2016", 2, "4/26/2016", 2, "4/27/2016")

What am I missing??我错过了什么??

你能不能使用这样的东西,并计算日期,dt1 和 dt2,使用日期 - dLast,而不是选择,然后尝试

.AutoFilter Field:=2, Criteria1:= ">=" & dt1, Operator:=xlAnd, Criteria2:="<=" & dt2

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

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