Sub CheckWeekday()
Dim inputDate As Date
inputDate = DateValue("2024-04-24")
If Weekday(inputDate, vbMonday) <= 5 Then
' Run code for weekday
MsgBox "It's a weekday!"
Else
MsgBox "It's not a weekday."
End If
End Sub
Sub CheckWeekday()
Dim inputDate As Date
inputDate = InputBox("Please enter the Date.", "Old Date", Default, XPos:=2880, YPos:=5760)
If Weekday(inputDate, vbMonday) <= 5 Then
' Run code for weekday
MsgBox "It's a weekday!"
Else
MsgBox "It's not a weekday."
End If
End Sub
I would add some validation in case the input is not a date or the users click Cancel :I made some minor changes, but you definitely got me on the right track !!!
Thank you.
VBA Code:Sub CheckWeekday() Dim inputDate As Date inputDate = InputBox("Please enter the Date.", "Old Date", Default, XPos:=2880, YPos:=5760) If Weekday(inputDate, vbMonday) <= 5 Then ' Run code for weekday MsgBox "It's a weekday!" Else MsgBox "It's not a weekday." End If End Sub
Sub CheckWeekday()
Dim inputDate As Variant
inputDate = InputBox("Please enter the Date.", "Old Date", Date, XPos:=2880, YPos:=5760)
If Not IsDate(inputDate) Then Exit Sub
If Weekday(inputDate, vbMonday) <= 5 Then
' Run code for weekday
MsgBox "It's a weekday!"
Else
MsgBox "It's not a weekday."
End If
End Sub
Sub CheckWeekday()
Dim inputDate As Variant
inputDate = InputBox("Please enter the Date.", "Old Date", Date, XPos:=2880, YPos:=5760)
If Not IsDate(inputDate) Then
MsgBox "That is not a valid date!", vbOKOnly, "TRY AGAIN!"
Exit Sub
End If
If Weekday(inputDate, vbMonday) <= 5 Then
' Run code for weekday
MsgBox "It's a weekday!"
Else
MsgBox "It's not a weekday."
End If
End Sub
I would also check if the user cancelled the inputbox and skip the MsgBox as follows:Or better yet...
Otherwise it is not easy to differentiate between an invalid date entry, and the code just not working properly since it isn't giving the user any indication of what is happening.VBA Code:Sub CheckWeekday() Dim inputDate As Variant inputDate = InputBox("Please enter the Date.", "Old Date", Date, XPos:=2880, YPos:=5760) If Not IsDate(inputDate) Then MsgBox "That is not a valid date!", vbOKOnly, "TRY AGAIN!" Exit Sub End If If Weekday(inputDate, vbMonday) <= 5 Then ' Run code for weekday MsgBox "It's a weekday!" Else MsgBox "It's not a weekday." End If End Sub
Sub CheckWeekday()
Dim inputDate As Variant
inputDate = InputBox("Please enter the Date.", "Old Date", Date, XPos:=2880, YPos:=5760)
If StrPtr(inputDate) = 0 Then Exit Sub
If Not IsDate(inputDate) Then
MsgBox "That is not a valid date!", vbOKOnly, "TRY AGAIN!"
Exit Sub
End If
If Weekday(inputDate, vbMonday) <= 5 Then
' Run code for weekday
MsgBox "It's a weekday!"
Else
MsgBox "It's not a weekday."
End If
End Sub
Sub CheckWeekday3()
Dim inputDate As Variant
inputDate = InputBox("Please enter the Date.", "Old Date", Date)
If StrPtr(inputDate) = 0 Then Exit Sub
If Not IsDate(inputDate) Then
MsgBox "That is not a valid date!", vbOKOnly, "TRY AGAIN!"
Exit Sub
End If
If Weekday(inputDate, vbMonday) <= 5 Then
' Run code for weekday
Worksheets("Sheet1").Range("H3").Value = inputDate
MsgBox "It's a weekday! - " & inputDate
Else
' Run code for weekend
Worksheets("Sheet1").Range("H3").Value = inputDate
MsgBox "It's not a weekday! - " & inputDate
End If
End Sub