2017-08-08 259 views
0

使用VBA可以获得当月的第一个星期一吗?我原以为下面将工作: -VBA当前月份的第一个星期一

DateSerial(Year(Date), Month(Date), ((7 - Weekday(DateSerial(Year(Date), Month(Date), 7))) + 2) Mod 7) 

...但如果我用这个今天例如,我得到的31/07/2017的日期,这显然是上个月。

+2

你的标题实在是完美的,你为什么不谷歌吗? – Vityata

+0

@Vityata如果能找到答案这样做,我不会在这里张贴... – user1936588

+1

没问题,我已经为你做了Google搜索:) – Vityata

回答

2

编辑: 这是一个可能的答案(so the first one in google is a false one):

Public Function FirstMonday(myDate As Date) As Date 

    Dim d As Date, w As Long 
    d = DateSerial(Year(myDate), month(myDate), 1) 
    w = Weekday(d, vbMonday) 
    FirstMonday = d + IIf(w <> 1, 8 - w, 0) 

End Function 

这就是你怎么称呼它:

Public Sub Test 
    debug.print FirstMonday(Now) 
End sub 

在一般情况下,这里是一个Excel Date Functions有趣的阅读。

+0

这篇文章是正确的,但最后一行需要是:'FirstMonday = d + IIf(w = 1,0,8-w)' – FloLie

+0

@FloLie - yup,'vbSunday'没有输入任何信息。 – Vityata

+1

@Vityata谢谢 – user1936588

0

您可以使用此泛型函数:

' Calculates the date of the occurrence of Weekday in the month of DateInMonth. 
' 
' If Occurrence is 0 or negative, the first occurrence of Weekday in the month is assumed. 
' If Occurrence is 5 or larger, the last occurrence of Weekday in the month is assumed. 
' 
' If Weekday is invalid or not specified, the weekday of DateInMonth is used. 
' 
' 2016-06-09. Gustav Brock, Cactus Data ApS, CPH. 
' 
Public Function DateWeekdayInMonth(_ 
    ByVal DateInMonth As Date, _ 
    Optional ByVal Occurrence As Integer, _ 
    Optional ByVal Weekday As VbDayOfWeek = -1) _ 
    As Date 

    Const DaysInWeek As Integer = 7 

    Dim Offset   As Integer 
    Dim Month   As Integer 
    Dim Year   As Integer 
    Dim ResultDate  As Date 

    ' Validate Weekday. 
    Select Case Weekday 
     Case _ 
      vbMonday, _ 
      vbTuesday, _ 
      vbWednesday, _ 
      vbThursday, _ 
      vbFriday, _ 
      vbSaturday, _ 
      vbSunday 
     Case Else 
      ' Zero, none or invalid value for VbDayOfWeek. 
      Weekday = VBA.Weekday(DateInMonth) 
    End Select 

    ' Validate Occurence. 
    If Occurrence <= 0 Then 
     Occurrence = 1 
    ElseIf Occurrence > 5 Then 
     Occurrence = 5 
    End If 

    ' Start date. 
    Month = VBA.Month(DateInMonth) 
    Year = VBA.Year(DateInMonth) 
    ResultDate = DateSerial(Year, Month, 1) 

    ' Find offset of Weekday from first day of month. 
    Offset = DaysInWeek * (Occurrence - 1) + (Weekday - VBA.Weekday(ResultDate) + DaysInWeek) Mod DaysInWeek 
    ' Calculate result date. 
    ResultDate = DateAdd("d", Offset, ResultDate) 

    If Occurrence = 5 Then 
     ' The latest occurrency of Weekday is requested. 
     ' Check if there really is a fifth occurrence of Weekday in this month. 
     If VBA.Month(ResultDate) <> Month Then 
      ' There are only four occurrencies of Weekday in this month. 
      ' Return the fourth as the latest. 
      ResultDate = DateAdd("d", -DaysInWeek, ResultDate) 
     End If 
    End If 

    DateWeekdayInMonth = ResultDate 

End Function 

然后:

FirstMonday = DateWeekdayInMonth(Date, 1, vbMonday) 
相关问题