jeudi 12 juillet 2018

Extract Date from list of Dates and switch on/off parameters based on criteria VBA Access

I'd like to come up with a function that checks a particular Date, against the below array of dates and returns the previous Year-End Date and the previous Month-End Date. In addition, i'd also like to perform two additional checks on a second date and return True/False based on (1) is Y_Date the exact Month-End date and (2) is Y_Date the exact Year-End date.

Here's the basic code I've come up with so far, however i'm wondering if there's a neater way of doing this.

DateArray = Array("29/12/2017", "29/06/2018", "31/07/2018", "31/08/2018", "28/09/2018", "31/10/2018", "30/11/2018", "31/12/2018", "31/01/2019", "28/02/2019", "29/03/2019", "30/04/2019", "31/05/2019", "28/06/2019", "31/07/2019", "30/08/2019", "30/09/2019", "31/10/2019", "29/11/2019", "31/12/2019")

L_Date = CDate("11/07/2018")
Y_Date = CDate("10/07/2018")

For Each e In DateArray
    If Month(CDate(e)) = (Month(L_Date) - 1) And Year(CDate(e)) = (Year(L_Date)) Then
        ME_Date = CDate(e)
    ElseIf Month(CDate(e)) = 12 And Month(L_Date) = 1 And Year(CDate(e)) = (Year(L_Date) - 1) Then
        ME_Date = CDate(e)
    Exit For
    End If
Next e

For Each e In DateArray
    If Month(CDate(e)) = 12 And Year(CDate(e)) = (Year(L_Date) - 1) Then
        YE_Date = CDate(e)
    Exit For
    End If
Next e

For Each e In DateArray
    MECheck = False
    YECheck = False
    If StrComp(CDate(e), CDate(Y_Date)) = 0 Then
        MECheck = True
        If Month(CDate(Y_Date)) = 12 Then
            YECheck = True
        End If
        Exit For
    End If
Next e

Essentially the process (by example of a case) should work like this:

Case:

L_Date = 15/11/2018 and Y_Date = 14/11/2018 returns the following:

  • ME_Date = 31/10/2018 (previous month end date)
  • YE_Date = 29/12/2017 (previous year end date)
  • MECheck = False (since Y_Date is not the 30/11/2018)
  • YECheck = False (since Y_Date is not the 31/12/2018)

Thank you for your help and any advice is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire