简体   繁体   English

按月过滤,如果日期是一年前?

[英]Filter by month and if a date was a year ago?

I am trying to make a report that pulls up monthly birthdays for people who have been at the company for longer than one year. 我正在尝试制作一份报告,该报告列出了在公司工作超过一年的人员的每月生日。 I am having an issue on figuring out how to do the logic to do what I want. 我在弄清楚如何执行所需的逻辑时遇到问题。 What I am doing is having the user select a month they want to search for, then it will filter out that month then select people who have been at the company longer than a year according to the month not the exact day. 我正在做的是让用户选择他们要搜索的月份,然后它将筛选出该月份,然后根据月份(而不是确切的日期)选择在公司工作超过一年的人员。 So if they would be here a year in two weeks but it is still in October for example, then I want to consider them being here a year to give them a birthday gift. 因此,如果他们将在两周之内到这里度过一年,但例如仍然在十月,那么我想考虑他们在这里一年来给他们生日礼物。

I want to filter my list so that I only have a list of people who have a birthday in the month I choose, and who have been here for at least a year. 我想过滤列表,以便只列出在我选择的月份中有生日并且已经在这里至少一年的人。 But here is the issue, For example October. 但这是问题所在,例如十月。 Right now I am using my current date to decide if it has been a year. 现在,我正在使用当前日期来确定是否是一年。 But I only want to run this once a month. 但是我只想每月运行一次。 So if they have a birthday in October AND if they were hired in October LAST YEAR then I want them on my list as well even if it technically hasn't been a year. 因此,如果他们在十月度过生日,并且如果他们在去年的十月被录用,那么即使从技术上来说还不是一年,我也希望他们也列入我的名单。 So if it would be a year according to what month they were hired, then I want to consider them a year and have them included. 因此,如果根据他们被雇用的月份是一年,那么我想将它们考虑为一年并包括在内。

I tried to explain it the best I could I am going in circles I think from thinking about this. 考虑到这一点,我试图尽我所能地向大家解释。 Here is some code that I have been working on but it does not do what I want. 这是我一直在努力的一些代码,但是它并没有实现我想要的功能。 It is close but this goes to the EXACT date of what day you run the report on. 截止日期已经到来,您可以在该日期开始生成报告。 I need it so if you have been here a year IN THAT MONTH, then you can stay on the list. 我需要它,所以如果您那个月来过这里一年,那么您就可以留在清单上。

With wb.Sheets(2)
    For lngRow = lngRows To 2 Step -1
        If Date - Range("F" & lngRow) < 365 Then
            wb.Sheets(2).Rows(lngRow).EntireRow.Delete
        End If
    Next
End With

在此处输入图片说明

The first table is raw data with people who have birthdays in October. 第一个表是原始数据,其中包含10月有生日的人。 The second table is who should be left if for example the actual date is October 15th or any date in October. 例如,如果实际日期是10月15日或10月中的任何日期,则应该剩下第二个表。 Everyone in October should still get a birthday card if they have been here a year. 如果十月的每个人都来过一年,那么他们仍然应该获得生日贺卡。

Any help is appreciated! 任何帮助表示赞赏! My brain hurts... 我的脑袋疼...

Using your provided sample data, something like this should work for you: 使用您提供的样本数据,类似这样的方法应该适合您:

Sub tgr()

    Dim ws As Worksheet
    Dim TargetDate As Date

    Set ws = ActiveWorkbook.Sheets(2)
    TargetDate = DateSerial(Year(Now()) - 1, Month(Now()) + 1, 1)

    With ws.Range("A1:D" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
        .AutoFilter
        .AutoFilter 2, "<" & TargetDate
        .AutoFilter 3, "=" & Month(Now())
    End With

End Sub

You might want to consider putting a date, say 10/28/17 , in E1 and in E2 and copied down to suit: 您可能要考虑在E1和E2中放置一个日期,例如10/28/17 ,并复制下来以适合:

=AND(B2<EOMONTH(E$1,-13),C2=MONTH(E$1))

Then you should be able to filter on ColumnE to select TRUE without further bother, other than changing E1 to suit. 然后,除了更改E1以适合之外,您应该能够在ColumnE上进行筛选以选择TRUE,而无需再费心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM