Consulting

Results 1 to 6 of 6

Thread: How to get dates of all mondays between 2 dates?

  1. #1

    How to get dates of all mondays between 2 dates?

    Hello all,

    Can anyone help me in getting an user define function to get monday dates between two dates?

    For Ex: Start date = 4/4/2020 & End date= 5/5/2020, then result should give me a list of Mondays dates

    dd/mm/yyyy
    dd/mm/yyyy
    dd/mm/yyyy
    ......so on

    Please help me this is very urgent..

    Thank u in advance.

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    I had a bit of time so my effort:

    Start date in A1, End date in A2 and Day you want in A3

    Sub GetDays()
        Dim i As Long, j As Long, ar, dy As Long, dys() As String
        dys = Split("su,mo,tu,we,th,fr,sa", ",")
        For i = 0 To 6
            If dys(i) = LCase(Left(Cells(3, 1), 2)) Then dy = i + 1
        Next
        If dy = 0 Then MsgBox "Invalid day!": Exit Sub
        ReDim ar(0)
        For i = Cells(1, 1) To Cells(2, 1)
            If Weekday(i) = dy Then
                ar(j) = i
                j = j + 1
                ReDim Preserve ar(j)
            End If
        Next
        Range("c2").CurrentRegion.ClearContents
        Range("c2:c" & j + 1) = WorksheetFunction.Transpose(ar)
    End Sub
    Semper in excretia sumus; solum profundum variat.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by ppatade242 View Post
    Hello all,

    Can anyone help me in getting an user define function to get monday dates between two dates?

    For Ex: Start date = 4/4/2020 & End date= 5/5/2020, then result should give me a list of Mondays dates

    dd/mm/yyyy
    dd/mm/yyyy
    dd/mm/yyyy
    ......so on

    Please help me this is very urgent..

    Thank u in advance.
    It's tricky getting a UDF (a WS function) to return an array with a varying number of elements

    Are you sure that you a UDF, and not a sub like paulked's?

    Maybe a sample workbook with the expected result will help
    ---------------------------------------------------------------------------------------------------------------------

    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

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by Paul_Hossler View Post
    It's tricky getting a UDF (a WS function) to return an array with a varying number of elements
    Are you sure that you a UDF, and not a sub like paulked's?
    Yes, it's tricky. In the attached there's a UDF called Mondays which is used thus:
    2020-04-20_122929.jpg (I used Nd for End because End is a reserved word.)

    It is committed to the sheet using Ctrl+Shift+Enter (rather than just Enter) and usually to a range of cells in one go. That range of cells can be a row or a column.
    I've handled the varying number of elements with a modicum of trial and error; If you've entered the formula into a range which is too large, you get the usual error cells at the end of the range, if the range is too small, the UDF reports how many more cells you need in the last cell:
    2020-04-20_123635.jpg
    [You could enter the formula into a single cell (no need even to array-enter for a single cell) and it will tell you how many more cells you need.]

    You have to remember this is a formula, so if you change the values in cells B1:C1 you'll get different results straight away, so if you need to keep these data then you should Copy, Paste-Special, Values, in situ.
    Attached Files Attached Files
    Last edited by p45cal; 04-20-2020 at 05:21 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    That's tidy!
    Semper in excretia sumus; solum profundum variat.

Posting Permissions

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