如何查找是月的第一天还是第二天?
我只是想看看是在某月的第一天,第二天还是少于第五天做某事。
If Date = Application.WorkDay(DateSerial(Year(Date), Month(Date), 0), 1) Or _
Date = Application.WorkDay(DateSerial(Year(Date), Month(Date), 0), 2) Or _
Date = Application.WorkDay(DateSerial(Year(Date), Month(Date), 0), 3) Then
MsgBox "1st, 2nd or 3rd day of month"
End If
End Sub
谢谢大家
答案 0 :(得分:0)
这里有一个功能可以测试日期(或者如果未指定,今天)是否在前X个工作天内:
Function date_is_in_X_days(firstXdays As Integer, Optional dt As Date) As Boolean
If dt = 0 Then dt = Date
date_is_in_X_days = False
With WorksheetFunction
For t = 1 To firstXdays
If dt = .WorkDay(.EoMonth(dt, -1), t) Then
date_is_in_X_days = True
Exit For
End If
Next
End With
End Function
要对此进行测试:
Sub test_function()
If date_is_in_X_days(3) Then MsgBox "1st, 2nd or 3rd day of month"
End Sub
答案 1 :(得分:0)
建议将DAY
用作@Warcupine。
Public Function date_is_in_X_days(DayNumber As Long, Optional DateValue As Date) As Boolean
If DateValue = 0 Then DateValue = Date
date_is_in_X_days = Day(DateValue) < DayNumber 'Compare the two values which returns TRUE or FALSE
End Function
然后可以用作:
Public Sub Test()
Debug.Print date_is_in_X_days(4) 'False if today is >= 4th.
Debug.Print date_is_in_X_days(2, DateValue("3-Sep-19")) 'False as 3rd is later than 2nd.
Debug.Print date_is_in_X_days(4, DateValue("3-Sep-19")) 'True as 3rd is earlier than 4th.
End Sub
或作为工作表公式,其中A1包含日期:
=date_is_in_X_days(5,$A$1)