自动过滤方法失败 - 运行时错误1004

时间:2015-07-23 13:34:03

标签: excel-vba autofilter vba excel

你好吗?

这是我的问题。我试图应用自动过滤器,其标准是日期,这取决于我们一年中的哪个季度。这是代码。

If fator = 1 Then
    DataInicio = DateSerial(Year(Date), 10, 1)
    DataFinal = DateSerial(Year(Date), 12, 31)
ElseIf fator = 2 Then
    DataInicio = DateSerial(Year(Date), 1, 1)
    DataFinal = DateSerial(Year(Date), 3, 31)
ElseIf fator = 3 Then
    DataInicio = DateSerial(Year(Date), 4, 1)
    DataFinal = DateSerial(Year(Date), 6, 30)
ElseIf fator = 4 Then
    DataInicio = DateSerial(Year(Date), 7, 1)
    DataFinal = DateSerial(Year(Date), 9, 30)
End If

wb.Sheets("Change-Order FUP").Activate

If wb.Sheets("Change-Order FUP").AutoFilterMode = True And wb.Sheets("Change-Order FUP").FilterMode = True Then
    wb.Sheets("Change-Order FUP").ShowAllData
ElseIf wb.Sheets("Change-Order FUP").AutoFilterMode = False Then
    wb.Sheets("Change-Order FUP").Cells(1, 1).AutoFilter
End If

wb.Sheets("Change-Order FUP").Range("A:P").AutoFilter Field:=5, Criteria1:=">="& DataInicio, Operator:=xlAnd, Criteria1:="<=" & DataFinal

我尝试使用Criteria1和Criteria2,Criteria1,Criteria2都没有用。有人可以帮我一把吗?

再次感谢您的支持!干杯

1 个答案:

答案 0 :(得分:0)

Autofilter可能会对日期格式挑剔。首先尝试转换为Double:

wb.Sheets("Change-Order FUP").Range("A:P").AutoFilter Field:=5, Criteria1:=">="& CDbl(DataInicio), Operator:=xlAnd, Criteria2:="<=" & CDbl(DataFinal)