简体   繁体   English

过去一个月至当月后两个月的VBA

[英]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列中,我们要在以下位置显示日期:

  • last month 上个月
  • this month 这个月
  • one month ahead 提前一个月
  • two months ahead. 提前两个月。

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: 结果:

在此处输入图片说明
WARNING : 警告

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.

相关问题 VBA在工作簿中的相似工作表上排序,而不使用基于月的命名工作表,该工作表会根据当月的月而变化 - VBA sort on similar sheets in a workbook without using named sheet based on months that will change based on current month 改进 VBA,它过滤当前月份 + 月份到年末的数据透视表字段 - Improve VBA which filters pivot table field with current month + months to the year end 如何在 VBA 中自动填充当月的一列,然后自动填充下一列? - How to Autofill a column in VBA for current month and Autofill next column for month after that? Excel公式可将日期标记为(1,0),从现在开始到过去1年的几个月。如过去3个月,过去6个月和年初至今 - Excel formula to fetch dates as a flag (1,0) for months from now into past 1 yr.As in Past 3month,Past 6 months and Year to date 将当前月份工作表拉到基于当前月份的新工作簿 - Pulling current months sheet to a new workbook based on the current month 在 VBA 代码中的文件夹路径中使用当前月份 - Using the Current Month in a Folder Path in VBA Code VBA使用公式拖动行,但仅当月 - VBA to drag a row with formula but for current month only 截至当月的VBA代码 - VBA Code for Filtering up through the current month VBA:以俄语获取当前月份 - VBA: Get current month in russian language Excel VBA datediff一起计算月,日和月? - Excel VBA datediff to Calculate Months, Days and Month together?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM