我有两列,即#34;状态"和"完成日期"。我想过滤" OPEN"和#34; CLOSED"首先在状态列中,然后过滤" 1/0/1900"在完成日期列。
为此,我写了以下代码:
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
With ActiveSheet
.AutoFilterMode = False
.Range("A1:A" & LR).AutoFilter Field:=1, Criteria1:=Array( _
"OPEN", "CLOSED"), Operator:=xlFilterValues
.Range("A1:A" & LR).AutoFilter Field:=2, Criteria1:=Array(0, "1/0/1900"), Operator:= _
xlFilterValues
End With
但是这没用。起初我得到错误(范围类的自动筛选方法失败1004)。然后我错误地删除了With语句中的第一行(.AutoFilterMode = False),我没有收到任何错误。但是,过滤器根本没有返回任何结果。
我认为我的日期过滤器出现问题,因为“状态”列过滤器工作较早。所以我研究并编写了下面的代码,仅用于日期过滤器,但它再次起作用。
Dim dDate As Date
Dim strDate As String
Dim lDate As Long, LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
dDate = DateSerial(1900, 1, 0)
lDate = dDate
ActiveSheet.Range("A1:A" & LR).AutoFilter Field:=2, Criteria1:=">=" & lDate, _
Operator:=xlAnd, Criteria2:="<" & lDate + 1
请帮助让这两个过滤器正常工作。
提前致谢。
答案 0 :(得分:1)
你搞砸了一点
试试这个
Sub Test()
With ActiveSheet '<--| reference your sheet
.AutoFilterMode = False '<--| remove any existing filtering
With Range("B1", .Cells(.Rows.count, 1).End(xlUp)) '<--| reference its range in columns A:B from row 1 down to column A last not empty row
.AutoFilter Field:=1, Criteria1:=Array("OPEN", "CLOSED"), Operator:=xlFilterValues '<--| filter on referenced range 1st column with "OPEN" or "CLOSED" values
.AutoFilter Field:=2, Criteria1:="1/0/1900" '<--| filter on referenced range 2nd column with "1/0/1900" value
End With
End With
End Sub
顺便说一句,你最好用ActiveSheet
代替Worksheets("mySheetName")
(你将“mySheetName”更改为实际的工作表名称),因为依赖ActiveSheet
可能会产生误导
答案 1 :(得分:0)
TarunS,看起来您只在Range()中选择一列(A列),但尝试在两列中应用过滤器。我创建了excel工作表,其中A列是“状态”,B列是“完成日期”,我可以使用以下代码创建过滤器: -
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
With ActiveSheet
.AutoFilterMode = False
.Range("A1:B" & LR).AutoFilter Field:=1, Criteria1:="=CLOSED", _
Operator:=xlOr, Criteria2:="=OPEN"
.Range("A1:B" & LR).AutoFilter Field:=2, Criteria1:="1/0/1900"
End With
请注意,我在A列和B列中有相同数量的项目,而我正在使用Excel 2010.如果您仍然遇到代码问题,请告诉我。
答案 2 :(得分:0)
我认为您的数据如下所示
column A Column B
Status Finish Date
OPEN 1/0/1900
CLOSED 1/0/1900
OPEN 1/0/1900
CLOSED 1/0/1900
OPEN 1/0/1904
CLOSED 1/0/1905
OPEN 1/0/1906
CLOSED 1/0/1907
OPEN 1/0/1908
CLOSED 1/0/1909
OPEN 1/0/1910
CLOSED 1/0/1911
OPEN 1/0/1900
CLOSED 1/0/1900
OPEN 1/0/1900
CLOSED 1/0/1900
OPEN 1/0/1900
CLOSED 1/0/1900
OPEN 1/0/1905
OPEN 1/0/1906
CLOSED 1/0/1906
OPEN 1/0/1906
CLOSED 1/0/1906
OPEN 1/0/1906
CLOSED 1/0/1906
OPEN 1/0/1906
CLOSED 1/0/1906
OPEN 1/0/1906
x 1/0/1906
y 1/0/1906
Z 1/0/1906
A 1/0/1906
b 1/0/1906
您可以在C栏中输入公式(在C2单元格处)
= IF(AND((OR(A2 =&#34;打开&#34;,A2 =&#34;关闭&#34;)),B2 =&#34; 1/0/1900&#34;) &#34;是&#34;&#34;否&#34)
将此公式拖到最后一行(A列或B列的最后一行)
它会为您提供如下输出:
Status Finish Date Result
OPEN 1/0/1900 Yes
CLOSED 1/0/1900 Yes
OPEN 1/0/1900 Yes
CLOSED 1/0/1900 Yes
OPEN 1/0/1904 No
CLOSED 1/0/1905 No
OPEN 1/0/1906 No
CLOSED 1/0/1907 No
OPEN 1/0/1908 No
CLOSED 1/0/1909 No
OPEN 1/0/1910 No
CLOSED 1/0/1911 No
OPEN 1/0/1900 Yes
CLOSED 1/0/1900 Yes
OPEN 1/0/1900 Yes
CLOSED 1/0/1900 Yes
OPEN 1/0/1900 Yes
CLOSED 1/0/1900 Yes
OPEN 1/0/1905 No
OPEN 1/0/1906 No
CLOSED 1/0/1906 No
OPEN 1/0/1906 No
CLOSED 1/0/1906 No
OPEN 1/0/1906 No
CLOSED 1/0/1906 No
OPEN 1/0/1906 No
CLOSED 1/0/1906 No
OPEN 1/0/1906 No
x 1/0/1906 No
y 1/0/1906 No
Z 1/0/1906 No
A 1/0/1906 No
b 1/0/1906 No
然后您可以轻松地将列c上的过滤器设置为条件&#34;是&#34; 或者如果要复制数据,请将过滤器设置为&#34; yes&#34;
并使用代码复制数据
范围(&#34; A2:C1048576&#34;)。specialcells(xlcellTypeVisible).copy目的地:=范围(&#34; D2&#34;)