Results 1 to 20 of 22

Thread: Count number of mondays

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,894
    Location
    I was playing around with something like Mark's


    Sub test()
        MsgBox CountMondays(#6/7/2016#, #6/10/2016#)
        MsgBox CountMondays(#6/6/2016#, #6/20/2016#)
        MsgBox CountMondays(#6/7/2016#, #6/20/2016#)
        MsgBox CountMondays(#6/6/2016#, #6/19/2016#)
        MsgBox CountMondays(#1/1/2016#, #12/31/2016#)
    End Sub
     
    Function CountMondays(ByVal StartingDate As Date, ByVal EndingDate As Date) As Long
        Dim d As Date, D1 As Date, D2 As Date
        Dim n As Long
         
        If Weekday(StartingDate) = vbMonday Then
            D1 = StartingDate
        Else
            D1 = StartingDate - Weekday(StartingDate) + vbMonday + 7
        End If
        
        If Weekday(EndingDate) = vbMonday Then
            D2 = EndingDate
        Else
            D2 = EndingDate - Weekday(EndingDate) + vbMonday - 7
        End If
             
        If D2 => D1 Then
            CountMondays = 1 + (CLng(D2) - CLng(D1)) \ 7
        Else
            CountMondays = 0
        End If
        
    End Function
    Last edited by Paul_Hossler; 06-03-2016 at 06:34 AM. Reason: updated code (again^2)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •