Consulting

Results 1 to 17 of 17

Thread: Check if weekend exist in date range

  1. #1
    VBAX Regular
    Joined
    Jul 2015
    Posts
    32
    Location

    Check if weekend exist in date range

    Hi,I have an excel sheet dates "From date" in column R and "To date" in column S. I want to know if there are week ends in between the date range.In some of the rows only the From date is available and To date may be blank.Can this be done using macro or if formula pls let me know how to apply in the vba code..- Tharabai

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Use this Formula in Row 2 of the column you want the results in. You can drag it down as many cells as needed.
    =HasWeekends($R2,$S2)
    and put this code in a Regular module.
    Public Function HasWeekEnds(FromDate As Range, ToDate As Range) As Boolean
    Dim Ds As Long
    Dim D As Long
    
    'Check various Cell formats
    If FromDate = "" Or ToDate = "" Then Exit Function
    If FromDate = 0 Or ToDate = 0 Then Exit Function
    
    Ds = DateDiff("d", FromDate, ToDate)
    
    For D = 0 To Ds
      If Weekday(FormDate.Value + D) = vbSunday _
        Or Weekday(FromDate.Value + D) = vbSaturday Then
        HasWeekEnds = True
        Exit Function
      End If
    Next D
    
    End Function
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If you have start date in A1 and end date in B1, this will return True/False "is there a Sat or Sun between the two dates, inclusive"
    =(B1-A1)>=(6-WEEKDAY(A1,2))
    Last edited by Aussiebear; 04-19-2023 at 12:40 AM. Reason: Added code tags

  4. #4
    VBAX Regular
    Joined
    Jul 2015
    Posts
    32
    Location
    Thank you so much for the coding..

    Can this function return the date of weekends instead on TRUE or FALSE.
    Also, my macro takes verylong time to run for the file which contains about 1lakh rows.

    Is there a way to speed up the results.

    -Tharabai

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    =A1+(6-WEEKDAY(A1,2))
    Will return the date that the next weekend begins.

    I.E if its is friday, that will return tommow's date
    if it is Saturday, that will return the same date
    If it is Sunday, it will return yesterday's date
    If it is Monday, it will return the date 5 days from then.
    Last edited by Aussiebear; 04-19-2023 at 12:41 AM. Reason: Added code tags

  6. #6
    Banned VBAX Newbie
    Joined
    Mar 2022
    Posts
    2
    Location

    Cool Erros make you --- > Form From > errrors I leeve in.

    Quote Originally Posted by SamT View Post
    Use this Formula in Row 2 of the column you want the results in. You can drag it down as many cells as needed.
    =HasWeekends($R2,$S2)
    and put this code in a Regular module.
    Public Function HasWeekEnds(FromDate As Range, ToDate As Range) As Boolean
    Dim Ds As Long
    Dim D As Long
    
    'Check various Cell formats
    If FromDate = "" Or ToDate = "" Then Exit Function
    If FromDate = 0 Or ToDate = 0 Then Exit Function
    
    Ds = DateDiff("d", FromDate, ToDate)
    
    For D = 0 To Ds
      If Weekday(FormDate.Value + D) = vbSunday _
        Or Weekday(FromDate.Value + D) = vbSaturday Then
        HasWeekEnds = True
        Exit Function
      End If
    Next D
    
    End Function

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    What appears to be the actual issue G6SGA? Are you complaining about the "errrors I leeve in" section. That is simply SamT's satirical signature to see if people actually read the post.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    Banned VBAX Newbie
    Joined
    Mar 2022
    Posts
    2
    Location
    Hi, and thanks for the pick up.
    I too was being a little satirical...
    With ERROS make you (yoda) [My deliberate error]
    Form & From is a miss spelling / dyslexia / auto correct; whatever, in the code itself. And I was noting his homework requirements.


    Stephen
    G6SGA

  9. #9
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Well Stephen, a non sensical post doesn't do anyone any great value. Perhaps you might like to apologise to SamT, given that he's a respected member of this community, whilst you are for all intents a wanker.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  10. #10
    VBAX Newbie
    Joined
    Mar 2022
    Posts
    1
    Location
    Hello, I am sorry if I offend anybody, including you Aussie bear that was not my intent.

    I was, wrongly obviously, trying to be humorous.

    Which for my first posts here did not go down too well.

    SamT -- Sorry again and thanks for the code which I am playing with and today is obviously a learning curve.

    P.S. If you wish to bar this email address too that is fine.


    Stephen

  11. #11
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    I am a self-proclaimed "not a very nice person" SoOOoo I tend to hover over the 'Post Quick Reply' button and think to myself "Is that diplomatic? - Could it offend?"

    This particular forum has a select number of close "Oldies " along with moderators that have been here a very long time and in my experience you get away with less here than you will elsewhere. Not that you should behave negatively anywhere.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by georgiboy View Post
    I am a self-proclaimed "not a very nice person" SoOOoo I tend to hover over the 'Post Quick Reply' button and think to myself "Is that diplomatic? - Could it offend?"

    This particular forum has a select number of close "Oldies " along with moderators that have been here a very long time and in my experience you get away with less here than you will elsewhere. Not that you should behave negatively anywhere.
    Interesting Georgi, I would have said the exact opposite. MrExcel is very cliquey, and if you aren't one of the gang, lo and behold. Excel Forum is over-moderated, sneeze and you're out. Excel Guru has many of the same moderators as Excel Forum, but we keep them a bit calmer. Here is the best of all IMO. Stephen post wasn't very useful I agree, but once he explained he thought he was being funny, he was crass at worst (again, IMO). Nothing to apologise for , certainly not to strike him off for, hopefully we will see a more positive contribution in future.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    @StephenT -- Well, today we learned that sometimes humor (or humour) doesn't travel too well.

    You explanation as to intent is accepted and we look forward to more meaningful contributions in the future



    PS - I'm glad there's a delete key on my phone since many times I'll look at a text and decide that it's not as funny as I thought
    ---------------------------------------------------------------------------------------------------------------------

    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

  14. #14
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Giving my post some thought (after reading Bob's post) I retract the below:
    in my experience you get away with less here than you will elsewhere.
    On the basis of:
    I am banned from Mr Excel
    I am banned from Excel Forum: Have a new account (with a strike)

    This is a good forum with nice people - I have been here since my journey of learning VBA started.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  15. #15
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    I also prefer the low testosteron/adrenalin driven fora, especially the German ones like e.g. Clever Excel Forum.

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by georgiboy View Post
    Giving my post some thought (after reading Bob's post) I retract the below:

    On the basis of:
    I am banned from Mr Excel
    I am banned from Excel Forum: Have a new account (with a strike)
    LOL! I am also banned from MrExcel, and I previously received a strike at Excel Forum, but I think it has lapsed now.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  17. #17
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location


    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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