IsDate

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,099
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Struck a problem in my macro... I have a string Variable (VarType=8). It's value is, e.g. 04 51 and IsDate tells me True.
It's not a real Date, it's a Time Duration (4 mins 51 secs). Excel may interpret it as a Date, but I don't want it to unless it's 'dd mmm yyyy' Format.

The variable is picked up from a text box, so I assume Vartype will always identify it as a string ?
I tried to have it fail (as a date) with CDate but that didn't work.

How best might I resolve this ? Perhaps just by LEN, but is there's a better way ?
Maybe IsDate can distinguish between a Time and Day-Month-Year somehow?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
The following UDF will return true if your string is in the 'dd mmm yyyy' format.

Code:
[/COLOR]Function xIsDate(s As String) As Boolean
Dim RX As Object: Set RX = CreateObject("VBScript.RegExp")
Dim Pat As String: Pat = "\d{2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{4}"


With RX
    .Global = True
    .MultiLine = True
    .ignorecase = True
    .Pattern = Pat
    xIsDate = .test(s)
End With


End Function


[COLOR=#333333]
 
Upvote 0
The following UDF will return true if your string is in the 'dd mmm yyyy' format.
Rich (BB code):
Function xIsDate(s As String) As Boolean
Dim RX As Object: Set RX = CreateObject("VBScript.RegExp")
Dim Pat As String: Pat = "\d{2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{4}"

With RX
    .Global = True
    .MultiLine = True
    .ignorecase = True
    .Pattern = Pat
    xIsDate = .test(s)
End With

End Function

Here is another way to write your function...
Code:
[table="width: 500"]
[tr]
	[td]Function xIsDate(S As String) As Boolean
  xIsDate = (S Like "## ??? ####") And (InStr("|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|", "|" & Mid(S, 4, 3) & "|") > 0)
End Function[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Thanks guys. Yes confirming the date is as expected is worth doing. I guess there's no way for Isdate to not interpret minutes and seconds as a date.
 
Upvote 0
how is this?
Code:
Function ItsADate(ByVal SomeThing As Variant) As Boolean


     ItsADate = False
     SomeThing = Trim$(SomeThing)
     If Len(SomeThing) > 0 Then
         If IsDate(SomeThing) Then
             ItsADate = CDate(SomeThing) > #1/1/2000# 'to stop times returning as dates??
         End If
     End If


End Function
 
Upvote 0
In the code I posted that trim is in the wrong place. And not needed anyway? Please delete that line entirely. Hence

Code:
Function ItsADate(ByVal SomeThing As Variant) As Boolean


     ItsADate = False
     If Len(SomeThing) > 0 Then
         If IsDate(SomeThing) Then
             ItsADate = CDate(SomeThing) > #1/1/2000# 'to stop times returning as dates??
         End If
     End If


End Function
 
Upvote 0
Hi Fazza, thanks - just about to try that out as I struck a snag with xIsDate. The problem was the value
passed might contain a date, not be just a date.
With your function does SomeThing have to be a Variant (it's the contents of a Text box),
and must the Date be > 2000 ? I guess I can try /change that and perhaps use CVar but just
wanted to ask.
 
Upvote 0
I dunno Fazza, as is "10 Jan 1999" returns False, but changing the Year (say to 1936)
makes things like "04 51" return True
 
Upvote 0
I dunno Fazza, as is "10 Jan 1999" returns False, but changing the Year (say to 1936)
makes things like "04 51" return True
It was a guess on my part that dates from year 2000 would be OK for you.

Do you know the earliest dates that could be in the data? Or the range of dates? Or range of times? Or something that distinguishes them.

If you have to work with dates from 19xx (that could overlap time entries), is there some other criteria - like string length - that can be used to distinguish time & date inputs? Then the function code can be changed to suit.

cheers

PS. If the entry might contain data other than a date, can you split the data into two separate entries. (On the user form?). So a date/time entry in one text box & the other data in another text box?
 
Last edited:
Upvote 0
The data comes from Access via an ADODB connection.
I ended up using If IsDate(frmCompare.aData) = True And Len(frmCompare.aData) > 6 Then
on the theory that my times are len(5) and anything more should be (a date).Hopefully it won't fall over under some unforeseen circumstances.
It was worth trying the other suggestions though, so thanks to everyone.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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