按月份仅筛选名称
在单元格A1,A2和A3中有三个date。 我想采取每个date的月份,并过滤包含多个date的数据集。 筛选器应该只包含在A1,A2和A3三个date每个月的date。
例如:
A1:01/01/2015,
A2:01/02/2015,
A3:01/03/2015
Dataset: 20/01/2015 25/12/2015 01/09/2015 14/02/2015 15/03/2015 01/06/2015
filter应该是:
20/01/2015 14/02/2015 15/03/2015
我目前的语法是这样的:
Dim mon1 As String Dim mon2 As String Dim mon3 As String Dim a As Date Dim b As Date Dim c As Date a = Range("A1") b = Range("A2") c = Range("A3") mon1 = MonthName(Month(a)) mon2 = MonthName(Month(b)) mon3 = MonthName(Month(c)) With ActiveSheet .AutoFilterMode = False .Range("A5:D5000").AutoFilter Field:=3, Criteria1:=Array(mon1, mon2, mon3), Operator:=xlFilterValues End With
不幸的是,似乎并没有工作。
-
在右侧插入一列,并将其命名为Month。
-
把这个公式
=TEXT(C6,"mmmm")
放在D6
,然后使用下面的代码
码
Sub Sample() Dim mon1 As String, mon2 As String, mon3 As String mon1 = MonthName(Month(Range("A1"))) mon2 = MonthName(Month(Range("A2"))) mon3 = MonthName(Month(Range("A3"))) With ActiveSheet .AutoFilterMode = False .Range("A5:D5000").AutoFilter Field:=4, _ Criteria1:=Array(mon1, mon2, mon3), _ Operator:=xlFilterValues End With End Sub
产量