Check IF Input Date is Weekday, THEN ... ELSE ...

JohnZ1156

Board Regular
Joined
Apr 10, 2021
Messages
160
Office Version
  1. 2021
Platform
  1. Windows
I'm looking for a simply macro to test if an Input date is a weekday or not.

Something Like:
If (Input date) is a Weekday, THEN

'Run this code

ELSE

'Run this code
End If
 
I think it is somehow recognizing that our European friends default date format is day/month/year.

Try this variation which will check and make sure the first part does not exceed 12:
VBA Code:
Sub CheckWeekday3()

    Dim inputDate As Variant
    Dim arr() As String

    inputDate = InputBox("Please enter the Date in mm/dd/yyyy format.", "Old Date", Date)
 
    If StrPtr(inputDate) = 0 Then Exit Sub
   
'   Check first part of date of entry (month)
    arr = Split(inputDate, "/")
    If arr(0) > 12 Then
        MsgBox "Invalid month entry of " & arr(0), vbOKOnly, "TRY AGAIN!"
        Exit Sub
    End If
 
    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
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
That make sense, those pesky Europeans.
But before, you could enter the date as either 4/25/2024 or 4-25-2024, and it worked!
Now, it will only accept dates if they use a "/" as the separator.
 
Upvote 0
OK, try this:
VBA Code:
Sub CheckWeekday3()

    Dim inputDate As Variant
    Dim delim As String
    Dim arr() As String

    inputDate = InputBox("Please enter the Date", "Old Date", Date)
  
    If StrPtr(inputDate) = 0 Then Exit Sub
    
'   Find what is being used as the date delimiter
    If InStr(1, inputDate, "/") > 0 Then
        delim = "/"
    Else
        If InStr(1, inputDate, "-") > 0 Then
            delim = "-"
        End If
    End If
    
'   Check to see if they used "/" or "-" as a delimiter
    If delim = "" Then
        MsgBox "Please enter date in mm/dd/yyyy or mm-dd-yyyy format", vbOKOnly, "TRY AGAIN!"
        Exit Sub
    End If
    
'   Check first part of date of entry (month)
    arr = Split(inputDate, delim)
    If arr(0) > 12 Then
        MsgBox "Invalid month entry of " & arr(0), vbOKOnly, "TRY AGAIN!"
        Exit Sub
    End If
  
    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
Hopefully, there aren't any other monkey wrenches to toss into this process!
 
Upvote 0
Solution
Great ! Thank you.
It's just that I have different people entering the data.
 
Upvote 0
Great ! Thank you.
It's just that I have different people entering the data.
You are welcome.

That is why in my previous post, I added this to the Input Box:
Rich (BB code):
    inputDate = InputBox("Please enter the Date in mm/dd/yyyy format.", "Old Date", Date)

But I suppose we all know how well people actually read and follow instructions these days!
 
Upvote 0
@JohnZ1156
My question is: If I'm entering the date as MM/DD/YYYY, why does it allow me to enter 13/1/2024 as a date? There is no 13th month?
Because 13/1/2024 is still a valid date. The vba IsDate function sees it as the 13th of Jan 2024.

13/1/2024 may not be in accordance with your default local system date format (mm/dd/yyy) which is what is causing the confusion.

In British English, dates are usually written in the order day – month – year, while in American English they are written month – day – year

Here is an alternative code which will only accept dates that are in accordance with the user's local date format.

VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
    Private Declare PtrSafe Function GetUserDefaultLCID Lib "kernel32" () As Long
#Else
    Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
    Private Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
#End If

Private Function GetSystemDateFormat() As String
    Const LOCALE_USER_DEFAULT = &H400, LOCALE_SSHORTDATE = &H1F
    Dim sBuffer As String * 128&, lRet As Long
    lRet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sBuffer, 128&)
    If lRet Then
        GetSystemDateFormat = Left$(sBuffer, lRet - 1&)
    End If
End Function


Sub CheckWeekday4()

    Dim inputDate As Variant, strFormat As String

    inputDate = InputBox("Please enter the Date.", "Old Date", Date)
  
    If StrPtr(inputDate) = 0 Then Exit Sub
  
    If Not IsDate(inputDate) Then
        GoTo ErrHandler
    End If
    
    inputDate = Replace(inputDate, "-", "/")
    
    inputDate = Right("00" & Split(inputDate, "/")(0), 2) & "/" & _
                Right("00" & Split(inputDate, "/")(1), 2) & "/" & _
                Split(inputDate, "/")(2)
                
    strFormat = GetSystemDateFormat
    
    If UCase(Left(strFormat, 1)) = "D" Then
        If Mid(inputDate, 4, 2) > 12 Then
            GoTo ErrHandler
        End If
    Else
        If UCase(Mid(inputDate, 1, 2)) > 12 Then
            GoTo ErrHandler
        End If
    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
    
    Exit Sub
    
ErrHandler:

    MsgBox "That is not a valid date!", vbOKOnly, "TRY AGAIN!"

End Sub
 
Last edited:
Upvote 0
Sorry, I just saw Joe's answer which is working fine.
If you still want to restrict the date input according to the user's local date format then here is yet another improved version which also validates the "/" character and informs the user of the expected format.

VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
#Else
    Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
#End If

Private Function GetSystemDateFormat() As String
    Const LOCALE_USER_DEFAULT = &H400, LOCALE_SSHORTDATE = &H1F
    Dim sBuffer As String * 128&, lRet As Long
    lRet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sBuffer, 128&)
    If lRet Then
        GetSystemDateFormat = Left$(sBuffer, lRet - 1&)
    End If
End Function

Sub CheckWeekday()

    Dim inputDate As Variant, strFormat As String

    inputDate = InputBox("Please enter the Date.", "Old Date", Date)
  
    If StrPtr(inputDate) = 0 Then Exit Sub
    
    strFormat = GetSystemDateFormat
  
    If Not IsDate(inputDate) Then
        GoTo ErrHandler
    End If
    
    inputDate = Replace(inputDate, "-", "/")
    
    If InStr(inputDate, "/") = 0 Then GoTo ErrHandler
    
    inputDate = Right("00" & Split(inputDate, "/")(0), 2) & "/" & _
                Right("00" & Split(inputDate, "/")(1), 2) & "/" & _
                Split(inputDate, "/")(2)
    
    If UCase(Left(strFormat, 1)) = "D" Then
        If Mid(inputDate, 4, 2) > 12 Then
            GoTo ErrHandler
        End If
    Else
        If UCase(Mid(inputDate, 1, 2)) > 12 Then
            GoTo ErrHandler
        End If
    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
    
    Exit Sub
    
ErrHandler:

    MsgBox "Please enter date in " & LCase(strFormat) & " format", vbOKOnly, "TRY AGAIN!"

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,457
Messages
6,124,941
Members
449,197
Latest member
k_bs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top