过滤器不接受日期变量

时间:2014-12-03 14:23:34

标签: excel vba excel-vba

尝试一下我复制并改编的vba。

这很好用

With ActiveSheet
        .AutoFilterMode = False
        rng1.AutoFilter Field:=1, Criteria1:="<>2014-12-02"
        rng1.Offset(1, 0).EntireRow.Delete
        .AutoFilterMode = False
    End With

我创建了一个名为factorydate

的变量
factorydate = Worksheets("systemdata").Cells(3, "C").Value

如何更换日期&#39; 2014-12-02&#39;变量factorydate?类似的东西:

With ActiveSheet
        .AutoFilterMode = False
        rng1.AutoFilter Field:=1, Criteria1:="<>" & factorydate
        rng1.Offset(1, 0).EntireRow.Delete
        .AutoFilterMode = False
    End With

1 个答案:

答案 0 :(得分:1)

如果.Cells(3, "C").Value中的值是日期(或Dim factorydate as Date),则由于日期默认排序,订单将更改数字。如果它是一个日期值,我会尝试:

rng1.AutoFilter Field:=1, Criteria1:="<>" & year(factorydate) & "-" & month(factorydate) & "-" & day(factorydate)

确保正确显示语法

编辑:或者这样做:

factorydate = Format(factorydate, "yyyy-MM-dd")
rng1.AutoFilter Field:=1, Criteria1:="<>" & factorydate

2EDIT:最终选项:

factorydate = Worksheets("systemdata").Cells(3, "C").Text

使用.Text返回确切的文本字符串,以确保您的过滤器正确应用。