=TODAY() Function not working with code for date criteria

依然范特西╮ 提交于 2021-01-29 07:09:15

问题


Below is the code I am working with to create a new table based on criteria (department and date) from an original table. The MaxDate works when I put in a specific date "8/4/2020", but not when I try and use the "=TODAY()" function. Any ideas?

Sub DepartmentSearch()
    
    Department = "IT"
    MaxDate = "=TODAY()"

    Set rng = ActiveSheet.UsedRange  ' source table
    rng.AutoFilter Field:=13, Criteria1:=Department  ' filter Department
    ActiveSheet.UsedRange.AutoFilter Field:=8, Criteria1:="<" & MaxDate  ' filter date
    rng.Copy  ' copy filtered rows
    Range("Z2").Select ' source table
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False  ' paste values to target table
    rng.AutoFilter ' turn off filter on source table

End Sub

回答1:


You are conflating formulas and VBA.

That said, just use Date:

MaxDate = Date


来源:https://stackoverflow.com/questions/63248574/today-function-not-working-with-code-for-date-criteria

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!