【问题标题】:First Monday of Current Month in VBAVBA中当月的第一个星期一
【发布时间】:2017-08-08 09:44:51
【问题描述】:

使用 VBA 是否可以获得当月的第一个星期一?我原以为下面会起作用:-

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

...但如果我今天使用它,我得到的日期是 2017 年 7 月 31 日,显然是上个月。

【问题讨论】:

  • 你的标题真的很完美,为什么不google一下?
  • @Vityata 如果能找到答案,我不会在这里发帖...
  • 没问题,我已经为你做了谷歌搜索:)

标签: vba ms-access


【解决方案1】:

编辑: 这是一个可能的答案(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 的有趣读物。

【讨论】:

  • 这个帖子没错,但最后一行需要是:FirstMonday = d + IIf(w = 1, 0, 8 - w)
  • @FloLie - 是的,vbSunday 没有收集任何信息。
  • @Vityata 谢谢
【解决方案2】:

你可以使用这个通用函数:

' 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)

【讨论】:

    【解决方案3】:

    运行以下宏,它会为您提供每月的第一个星期一:

    Sub PromptFirstMondayofTheCurrentMonth()
    
    Dim a As Variant
    
    a = Date - Day(Date) + 1
    
    MsgBox IIf(Weekday(a, vbMonday) = 1, 1, 9 - Weekday(a, vbMonday))
    
    End Sub 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-23
      • 2014-07-21
      • 2011-12-05
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      相关资源
      最近更新 更多