• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

ElseIf Weekday Exit Sub vba

Excelnoub

Member
Good day,
I’ve been on this for a couple of days. I can figure out how I would take this code and rule out the weekend date.
I have a userform that checks for ElseIf IsEmpty, then I would like to add the following but don’t know how I would puzzle them together:

Code:
ElseIf Cells(ActiveCell.row, 10) = Weekday(date1) = vbSaturday And Weekday(date1) = vbSunday Then
        MsgBox "Date cannot fall on a weekend, please change the Actual Date"
        Range("J" & ActiveCell.row).Select
    Unload Me
  
    Application.EnableEvents = True
    Exit Sub

I need to check if my ActiveCell.row, 10 falls on a weekend then to populate the message then select the range as it is doing but it's the first line that I can't figure out.
If someone would know this code I would be grateful
 
Hi,

The way the conditions in your ElseIf statement resolve themselves isn't what you want but, more importantly, the intention of your "And" condition can't ever be true because the weekday of date1 cannot be both a Saturday and a Sunday.

I'm not sure about the validity of doing the cell value check to the weekday of date1, but essentially I think you want your code to read like this:
Code:
ElseIf Cells(ActiveCell.Row, 10).Value = Weekday(date1) _
    And (Weekday(date1) = vbSaturday Or Weekday(date1) = vbSunday) Then
 
Found this:

I have added this to a module:

Code:
Public Function IsWeekend(InputDate As Date) As Boolean
    Select Case Weekday(InputDate)
        Case vbSaturday, vbSunday
            IsWeekend = True
        Case Else
            IsWeekend = False
    End Select
End Function

And now my Userform can hold the following code:

Code:
     ElseIf IsWeekend(Cells(ActiveCell.row, 10)) Then
        MsgBox "Date cannot fall on a weekend, please change the Actual Date"
        Range("J" & ActiveCell.row).Select
    Unload Me
   
    Application.EnableEvents = True
    Exit Sub

hope this helps someone in the future :)
 
Back
Top