[英]VBA for past month to 2 months after current month
I am trying to get more into VBA as it allows a lot more freedom. 我正在尝试更多地进入VBA,因为它允许更多的自由。 What I am trying to figure out is how can I do a VBA to filter a column to show only values that only occur last month, current month and two months in the future.
我要弄清楚的是如何执行VBA来过滤列以仅显示仅在上个月,本月和将来两个月出现的值。
I know I can use xlFilterLastMonth but right now I am still just recording macros and then changing them to fit my more specific needs. 我知道我可以使用xlFilterLastMonth,但现在我仍然只是记录宏,然后更改它们以满足我的更具体需求。 I can find singular filters using xlFilterNextMonth or xlFilterLastMonth.
我可以使用xlFilterNextMonth或xlFilterLastMonth查找单个过滤器。 But I can't really find anything for what I am looking for.
但是我找不到真正想要的东西。
Here is my current Macro: 这是我当前的宏:
Sub BASICPQS()
'
' BASICPQS Macro
'
'
Sheets("PRODUCTION").Range("$A$1:$AA$146").AutoFilter Field:=9, Operator:= _
xlFilterValues, Criteria2:=Array(1, "3/31/2016", 1, "4/27/2016")
ActiveWindow.SmallScroll Down:=-18
End Sub
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks.
谢谢。
Say we have data like: 假设我们有以下数据:
Dates
2/27/2016
5/28/2016
1/30/2016
5/3/2016
2/13/2016
4/29/2016
2/3/2016
7/5/2016
5/23/2016
3/11/2016
7/3/2016
2/5/2016
5/4/2016
6/20/2016
6/26/2016
7/14/2016
5/9/2016
1/26/2016
2/14/2016
3/9/2016
4/1/2016
2/18/2016
3/7/2016
In column A and we want to display dates in: 在A列中,我们要在以下位置显示日期:
These are dates greater than or equal to 3/1/2016 and less than or equal to 6/30/2016 if today is 4/20/2016. 这些日期是大于或等于2016年3月1日,如果今天是2016年4月20日,则小于或等于6/30/2016年。
Consider: 考虑:
Sub Macro1()
Dim d1 As Date, d2 As Date
Dim cr1 As String, cr2 As String
d1 = DateSerial(Year(Now), Month(Now) - 1, 1)
d2 = DateSerial(Year(Now), Month(Now) + 3, 0)
MsgBox d1 & vbCrLf & d2
MsgBox CLng(d1) & vbCrLf & CLng(d2)
cr1 = ">=" & CStr(CLng(d1))
cr2 = "<=" & CStr(CLng(d2))
ActiveSheet.Range("$A$1:$A$24").AutoFilter Field:=1, Criteria1:=cr1, _
Operator:=xlAnd, Criteria2:=cr2
End Sub
The result: 结果:
You must test this further to see if it works if today is in January or December. 您必须对此进行进一步测试,以查看今天是否是一月或十二月。 That is, can the code look ahead to next year or back to the previous year.
就是说,代码可以向前看还是明年。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.