好吧,我必须承认我设计的代码与你的完全不同,因为在 Excel 上处理日期更容易,所以我在 Access 中添加了对 Excel 的引用。
之后,我使用 Excel 中的自然 WorksheetFunctions 设计了 4 个函数来获取每种情况下的自然日期(上一个年末、上一个月末、本月末和本年末)。
Private Function PREV_YEAR_ED(ByVal ThisDate As Date) As Date
PREV_YEAR_ED = CDate("31/12/" & Year(WorksheetFunction.EDate(CDate(ThisDate), -12)))
End Function
Private Function PREV_MONTH_ED(ByVal ThisDate As Date) As Date
PREV_MONTH_ED = CDate(WorksheetFunction.EoMonth(CDate(ThisDate), -1))
End Function
Private Function MONTH_ED(ByVal ThisDate As Date) As Date
MONTH_ED = CDate(WorksheetFunction.EoMonth(CDate(ThisDate), 0))
End Function
Private Function YEAR_ED(ByVal ThisDate As Date) As Date
YEAR_ED = CDate("31/12/" & Year(ThisDate))
End Function
之后,我使用您发布的数据设计了宏。布尔检查很容易得到False 或True 只需一个简单的If。
但是要获取上一个年末日期和上一个月末日期,您需要循环所有日期。从数组中的日期,您需要得到最接近您的数据的日期,并且必须等于或小于您的数据。
因此代码检查日期之间的差异,存储具有日期之间最小差异的数组索引,并返回该值。
Sub MACRO()
Dim DateArray As Variant
Dim L_Date, Y_Date, ME_Date, YE_Date, TargetDate As Date
Dim MECheck, YECheck As Boolean
Dim i As Integer
L_Date = CDate("15/11/2018")
Y_Date = CDate("14/11/2018")
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")
'AVAILABLE FUNCTIONS
'PREV_YEAR_ED will get the natural last day of the previous year, according to the value of ThisDate
'PREV_MONTH_ED will get the natural last day of the previous month, according to the value of ThisDate
'MONTH_ED will get the natural last day of current month, according to the value of ThisDate
'YEAR_ED will get the natural last day of current year, according to the value of ThisDate
'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)
Dim DifDays, MinDifDays As Integer
Dim idx As Integer
'to get ME_Date
TargetDate = PREV_MONTH_ED(L_Date)
'ME_DATE will be a date on the array with minimun difference between natural previous month end date (TargetDate)
' and smaller or equal than TargetDate. We need to find with index of the array has the value we want
MinDifDays = 999
For i = 0 To UBound(DateArray) Step 1
If TargetDate = CDate(DateArray(i)) Then
idx = i
Exit For
ElseIf TargetDate > CDate(DateArray(i)) Then
DifDays = L_Date - CDate(DateArray(i))
If DifDays < MinDifDays Then
MinDifDays = DifDays
idx = i
End If
End If
Next i
ME_Date = CDate(DateArray(idx))
'to get YE_Date
TargetDate = PREV_YEAR_ED(L_Date)
'YE_Date will be a date on the array with minimun difference between natural previous year end date (TargetDate)
' and smaller or equal than TargetDate. We need to find with index of the array has the value we want
MinDifDays = 999
For i = 0 To UBound(DateArray) Step 1
If TargetDate = CDate(DateArray(i)) Then
idx = i
Exit For
ElseIf TargetDate > CDate(DateArray(i)) Then
DifDays = L_Date - CDate(DateArray(i))
If DifDays < MinDifDays Then
MinDifDays = DifDays
idx = i
End If
End If
Next i
YE_Date = CDate(DateArray(idx))
'boolean checks
If Y_Date = MONTH_ED(Y_Date) Then
MECheck = True
Else
MECheck = False
End If
If Y_Date = YEAR_ED(Y_Date) Then
YECheck = True
Else
YECheck = False
End If
Debug.Print ME_Date
Debug.Print YE_Date
Debug.Print MECheck
Debug.Print YECheck
Erase DateArray
End Sub
Private Function PREV_YEAR_ED(ByVal ThisDate As Date) As Date
PREV_YEAR_ED = CDate("31/12/" & Year(WorksheetFunction.EDate(CDate(ThisDate), -12)))
End Function
Private Function PREV_MONTH_ED(ByVal ThisDate As Date) As Date
PREV_MONTH_ED = CDate(WorksheetFunction.EoMonth(CDate(ThisDate), -1))
End Function
Private Function MONTH_ED(ByVal ThisDate As Date) As Date
MONTH_ED = CDate(WorksheetFunction.EoMonth(CDate(ThisDate), 0))
End Function
Private Function YEAR_ED(ByVal ThisDate As Date) As Date
YEAR_ED = CDate("31/12/" & Year(ThisDate))
End Function
如果我使用 L_Date = 15/11/2018 和 Y_Date = 14/11/2018 执行此代码,则调试器返回:
很抱歉编写了完全不同的代码,但我希望它可以提供帮助。