在Excel 2007中编码文本过滤器

时间:2009-05-21 02:39:49

标签: excel filter autofilter excel-vba vba

我正在尝试使用代码根据当前日期动态过滤电子表格。

我将我需要过滤的日期存储为“CurrDay”,我试图将存储的日期重新调回我的过滤算法。它无法正常工作,我需要弄清楚如何完成此代码。每次运行代码时,它都会在过滤器中返回CurrDay名称,而不是存储在CurrDay变量下的日期。

我在这里遗漏了一些东西,我需要一些方向。任何帮助表示赞赏。

代码:

Sub Finishing_A59_Filter()
'
' Finishing_A59_Filter Macro

' This macro will activate the A59 and Filter it properly for standard orders
' 

'This macro does not include the VMI's and APS orders in the code
' 
'

Dim Currday As Date

    Currday = Date + 7
    UName = Application.UserName


    Workbooks.Open Filename:="G:\Copy Modified A59 5-19-2009.xlsm", UpdateLinks _
        :=0
    Range("M2").Select
    ActiveCell.Value = Currday

    Columns("Q:Q").Select
    Selection.NumberFormat = "mm/d/yyyy"

    ' Filter the sheet to remove VMI's and APS orders

    ActiveSheet.Range("$A$3:$AA$2941").AutoFilter Field:=23, Criteria1:=Array( _
        "01", "04", "06", "08", "09", "10", "15", "25", "="), Operator:=xlFilterValues

   ' Set the proper date range for the sheet - This needs to be seven days beyond the current date

    ActiveSheet.Range("$A$3:$AA$2941").AutoFilter Field:=17, Criteria1:= _
        "<=Currday", Operator:=xlAnd

1 个答案:

答案 0 :(得分:1)

您需要将变量与条件字符串连接起来。

ActiveSheet.UsedRange.AutoFilter Field:=17, Criteria1:= _
        "<=" & Currday, Operator:=xlAnd

此外,最好使用“UsedRange”而不是组成一个大范围,因为如果你的数据超出你的任意范围它就不起作用,如果没有,就浪费资源。